ruby on rails 3 - How do I render partial via ajax in rails3 and jQuery -
i sth in rails 2:
def show_rec_horses rec_horses = horses.find(:all) page["allhorses"].replace_html :partial => "horses/recommended", :locals => {:rec_horses => rec_horses} end
how do in rails3 jquery. how can replace html of div partial via ajax call(link_to :remote => true) in rails3.
i tried sth (in show_rec_horses.js.erb):
$("#interactioncontainer").update("<%= escape_javascript(render("horses/recommended"))%>");
and nothing happens. tried other variations none worked. doing wrong? should approached in other way? answer appreciated.
okay, let's start controller.
def show_rec_horses @rec_horses = horses.find(:all) respond_to |format| format.html # show_rec_horses.html.erb format.js # show_rec_horses.js.erb end end
this make sure correct templates being rendered either html request or javascript request. if need respond javascript request, delete html part.
now let's have page containing following:
<p><%= link_to 'show horses', show_rec_horses_path, remote: true %></p> <div id="interactioncontainer"></div>
clicking link make request show_rec_horses action in controller, action render javascript file show_rec_horses.js.erb
.
this javascript file should contain this:
$('#interactioncontainer').html('<%=j render partial: 'horses/reccommended', locals: { rec_horses: @rec_horses } %>')
now container filled content of horses/_reccommended.html.erb
, example contain:
<% rec_horses.all.each |rec_horse| %> <p><%= rec_horse.name %></p> <% end %>
Comments
Post a Comment