Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6862167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:37:42+00:00 2026-05-27T02:37:42+00:00

Using an <% if/else/elsif/end %> statement, I detect whether an object exists in a

  • 0

Using an <% if/else/elsif/end %> statement, I detect whether an object exists in a User’s profile in my Rails 3 app. Depending on the object’s existence, I render one of two forms:

  • If the object exists, the form that is rendered is associated with the Controller’s remove_object. It is styled to look like a depressed button, and its submit value is "-".
  • If it doesn’t, the form that is rendered is associated with the Controller’s create_object. It is styled to look like a normal button, but its submit value is "+".

I style the forms differently to make the object’s existence clear to the user. While the <% if/else/elsif/end %> statement works great, I’d like to avoid refreshing the page to change which form is rendered. I have Ajax working on the forms themselves, but am not sure how to change the styling using .ajaxSuccess or some other jQuery method.

Here’s an <% if/else/elsif/end %> statement I use:

<% if current_user.profile.higher_ed == @user.profile.higher_ed %>
...
<% end %>
<% elsif current_user.profile.lists.exists?(:category => 'academic', :name => @user.profile.higher_ed) %>
<%= form_tag url_for(:controller => "profiles", :action => "remove_academic", :method => :delete), :id =>'remove_form', :remote => true do %>
  <%= hidden_field_tag :name, @user.profile.higher_ed %>
  <%= submit_tag "-", :class => 'button gray' %>
<% end %>
<% else %>
<%= form_tag url_for(:controller => "profiles", :action => "create_academic"), :id => "add_form", :remote => true do %>
  <%= hidden_field_tag :name, @user.profile.higher_ed %>
  <%= submit_tag "+", :class => 'button white' %>
<% end %>
<% end %>

What I’d like to do is, if the <% if/else/elsif/end %> statement shows add_form, once the create_academic action is finished, add_form is replaced with remove_form. And vice versa if the <% if/else/elsif/end %> statement shows remove_form. Is this possible? I’m not sure if my <% if/else/elsif/end %> statement will cause problems here or not.

UPDATE: Here is my create_academic.js.erb:

$('ul#goalsEdu').append("<%= escape_javascript(render(@goal)) %>");

Here’s the jQuery I was working with:

$(function(){
   $("form#add_goal").show()
   .ajaxStart(function(){
       $(this).show();
   })
   .ajaxStop(function(){
       $(this).replaceWith("<%= escape_javascript(render :partial => 'profiles/_remove_major_form')");
   })
});

So I just have to incorporate that into the create_academic.js.erb?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:37:42+00:00Added an answer on May 27, 2026 at 2:37 am

    Best way to do this is with unobtrusive javascript.

    Set up your create_academic and remove_academic action to respond_to js (it should already do that since you are using ajax). Now create a remove_academic.js and a create_academic.js file in the views folder for your profiles controller.

    This js file will get called whenever ajax is sent to those actions. So all you need to do is select with jquery the form_tag you want to change and render the opposite form from what it was before.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the benefit/downside to using a switch statement vs. an if/else in C#.
I'm using Cancan, Devise, Rails 3 for my ordering application. Each user has many
I was wondering what are the benefits of using anything else but Java for
I am writing an if/else alternative using a table driven method. I have the
Using strictly SQL (no PHP or anything else), is it possible to create a
Has anyone else had any problems using google's Domain Tracking API, I am specifically
Something else perhaps? I am already using nHibernate, but I get occasional issues where
Anyone else get this or suffer from this? I am using Vista and VS
Im using the following JS <a href=# onClick=if($(this).next('div').css('display') == 'none') { $(this).next('div').show('fast'); } else
I'm using this: jQuery('.class1 a').click( function() { if ($(.class2).is(:hidden)) { $(.class2).slideDown(slow); } else {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.