I have a quick question about render partial functionality. I have an
array of items and I am printing out the item and its link - I would
like the link when clicked to display the content below it. I have
this working so it goes to another page but I would like to print the
contents of _show.rhtml in a div tag below it. I guess the issue I am
having is how do I get item.id to be passed below?
<% @items.each do |item| %>
#this links to show.rhtml and renders the content it should
<%= link_to item.title, :action => “show”, :id => item.id %>
<% end %>
<%= render :partial => "show", :id => item.id %>
you need to either use an ajax call to update the contents of the page
with the partial, or you can render all the partials and then use
javascript to toggle (show/hide) the container div. Ajax is probably
the better solution, since you’re loading content only when the user
requests it, rather than the Javascript approach which requires you to
load the information for every item at once.
If you want to use the ajax method, you’ll need to use link_to_remote
instead of simply link_to, then define an Ajax callback method in your
controller, and then call
render :update do |page|
page.replace.html ‘my_div’, ‘my_partial_name’
end
search for link_to_remote on api.rubyonrails.org for more information
I just around to looking at it now but ‘link_to_remote’ is exactly
what I needed. It works like a charm, thanks a lot.