Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I render partial via ajax in rails3 and jQuery

Tags:

I could do sth like this 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 I do this in Rails3 with jQuery. So how can I replace html of my div with a partial via ajax call(link_to :remote => true) in Rails3.

I tried sth like (in my show_rec_horses.js.erb):

$("#interactionContainer").update("<%= escape_javascript(render("horses/recommended"))%>"); 

And nothing happens. I tried some other variations but none worked. What am doing wrong? Should this be approached in some other way? Any answer would be greatly appreciated.

like image 222
necker Avatar asked Feb 24 '11 00:02

necker


People also ask

What is textStatus in AJAX?

The textStatus is the textual status message returned by the server. The errorThrown parameter is the error thrown by jQuery. The callback function passed to the always() function is called whenever the AJAX request finishes, regardless of whether or not the AJAX request succeeds or fails.

How does AJAX work in jQuery?

AJAX = Asynchronous JavaScript and XML. In short; AJAX is about loading data in the background and display it on the webpage, without reloading the whole page. Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook tabs. You can learn more about AJAX in our AJAX tutorial.

What is render in AJAX?

Attribute render contains a space-separated list of HTML identifiers of the elements that will be updated on the web page once the AJAX response is received. It also supports four special values as execute attribute but the default value is @none.

What is render partial in rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.


1 Answers

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 %> 
like image 173
Arjan Avatar answered Oct 11 '22 03:10

Arjan