Take the 2-minute tour ×

I recently started with Rails 3.2 and am stuck while trying to implement some ajax functionality in my app. I followed this railscast completely (http://railscasts.com/episodes/240-search-sort-paginate-with-ajax). Beyond this I want to implement a shortlist button for each product which lets user shortlist products and store them in the session. I also want a small list of shortlisted products to show up on the same page, which needs to be ajax updated.

I am wondering what is the best way to do that. I currently implemented the link_to buttons with remote tag and a helper function to change the link to shortlist/unshortlist. I also, used a conditional div to show the shortlist based on the length of shortlist. However, the issue is that whenever I shortlist, the order of the products table is also reset.

Here are snippets of my code :-


def shortlist_unshortlist_link(product_id )
  if (user_session.already_shortlisted? product_id )
    link_to 'Unshortlist', { action: 'unshortlist', id: product_id  }, remote => 'true'
    link_to 'Shortlist', { action: 'shortlist', id: product_id  }, remote => 'true'

def shortlist_div
   if (user_session.shortlist.length > 0)
     render :partial => 'pages/shortlist_fields'


<div id="product">
 <% @products.each do |product| %>
<td><%= shortlist_unshortlist_link(product.id.to_s) %></td>
<div class="shortlist"><%= shortlist_div() %> </div>


def shortlist
 redirect_to products_path

def unshortlist
 redirect_to products_path

I think, the issue is because of redirect_to, but I am not getting how to avoid this without hurting the functionality. Am I on a totally wrong path here. Is this a good way to implement this. Any suggestions.

thanks, Amit

share|improve this question

2 Answers 2

up vote 0 down vote accepted

you should use in shortlist method,

respond_to do |format|
  format.html {redirect_to products_path }#shortlist.html.erb
  format.js #shortlist.js.erb

and write your java script to #shortlist.js.erb file.

Do the same with unshortlist.

share|improve this answer
this doesnt seem to work. Should there be something in my shortlist/unshortlist.html.js.erb . –  Amit Gupta Dec 2 '12 at 16:47
of course, there should be javascript code to manipulate the html. As well if you want to do any other processing specific to ajax request; you can write in format.js{# rails code here} block. –  maximus Dec 2 '12 at 16:52
I put in the code as suggested by you and code4j. However, its still not working. It only works when I redirect the format.js request to products_path. Or without it, when I reload the page. –  Amit Gupta Dec 3 '12 at 8:13
what you wrote to js.erb file? –  maximus Dec 3 '12 at 8:38
$('#shortlist').html('<%= escape_javascript(render :partial => 'pages/shortlist_fields') %>') in shortlist.js.erb –  Amit Gupta Dec 3 '12 at 8:42

I agree with Sush, you didn't response to the ajax request from link_to.

In your controller, shortlist method, response to the ajax request

respond_to do |format|
  format.html {redirect_to products_path }

By convention in Rails, format.js will execute the js.erb file with the same name as your method.

And in the shortlist.js.erb, you may write something like:

$('#shortlist').html('<%= escape_javascript(render "pages/shortlist_fields")%>');

Besides, you can also call the same js.erb file.

In the unshortlist method, you can do it like that:

respond_to do |format|
  format.html {redirect_to products_path }
  format.js {render :action=>'shortlist'}
share|improve this answer

Your Answer


