Search code examples
ajaxruby-on-rails-3ujsrespond-to

Respond_to many AJAX calls from same controller


I have a page which has 2 or more lists which I want to paginate (with kaminari for instance)

@list_a and @list_b

these are being created in my pages#home controller

I want to have a link for each list (with remote: true) to append new elements via javascript.

That would make

respond_to do |format|
format.js
..
end

will fire a home.js.erb call.

but in home.js.erb I don't know how to know which link made the home.js.erb fire.

How do I differentiate between the 2 ajax links - from the controller or in the js? and how?


Solution

  • try this,

    in view file

    <%= link_to "link_1", <your path>, :list => 1, remote: true %>
    <%= link_to "link_2", <your path>, :list => 2, remote: true %>
    

    in home.js.erb

    <% if params[:list] == "1" %>
       // link 1 clicked
    <% elsif params[:list] == "2" %>
       // link 2 clicked     
    <% end %>