Okay, let's start with the controller.
def show_rec_horses
@rec_horses = Horses.find(:all)
respond_to do |format|
format.html # show_rec_horses.html.erb
format.js # show_rec_horses.js.erb
end
end
This will make sure the correct templates are being rendered for either a html request or a javascript request. If you just need to respond to a javascript request, delete the html part.
Now let's say you have a page containing the following:
<p><%= link_to 'Show Horses', show_rec_horses_path, remote: true %></p>
<div id="interactionContainer"></div>
Clicking the link will make a request to the show_rec_horses action in your controller, this action will render the javascript file show_rec_horses.js.erb
.
This javascript file should contain something like this:
$('#interactionContainer').html('<%=j render partial: 'horses/reccommended', locals: { rec_horses: @rec_horses } %>')
Now your container will be filled with the content of horses/_reccommended.html.erb
, which for example could contain:
<% rec_horses.all.each do |rec_horse| %>
<p><%= rec_horse.name %></p>
<% end %>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…