Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails 3: AJAX call not refreshing div with link_to
    primarykey
    data
    text
    <p>I am new to Ruby on Rails and i am working through a few example applications in the O'Reilly Head First Rails book. In one of the examples there is a page made up of three partials. The middle partial is a list of items. There is a link right below this section that, when clicked, should refresh the div containing that partial. The book is running examples based off of Rails 2.3 i believe and i am using Rails 3.1. This is the example that the book is giving me:</p> <p>routes.rb:</p> <pre><code>map.connect '/flights/:flight_id/seats', :action=&gt;'flight_seats', :controller=&gt;'seats' </code></pre> <p>seats_controller.rb:</p> <pre><code>def flight_seats @flight = Flight.find(params[:flight_id]) render :partial =&gt; "flights/seat_list", :locals =&gt; {:seats =&gt; @flight.seats} end </code></pre> <p>show.html.erb:</p> <pre><code>&lt;div id="seats"&gt; &lt;%= render :partial=&gt;"seat_list". :locals=&gt;{:seats=&gt;@flight.seats} %&gt; &lt;/div&gt; &lt;$= link_to_remote("Refresh Seats", :url=&gt;"/flights/#{@flight.id}/seats", method=&gt;"get", :update=&gt;"seats") %&gt; </code></pre> <p>This example is also using prototype.js since that's what Rails 2.3 came with built in. Rails 3 has jQuery as the default JavaScript library. (not sure if that makes a big difference)</p> <p>Here is what i have so far. This is getting the contents of the partial correctly, it's just not updating the "seats" div after the AJAX call gets the partial. My code:</p> <p>routes.rb:</p> <pre><code>match 'flights/:flight_id/seats' =&gt; 'seats#flights_seats' </code></pre> <p>seats_controller.rb:</p> <pre><code>def flights_seats @flight = Flight.find(params[:flight_id]) render :partial =&gt; "flights/seat_list", :locals =&gt; { :seats =&gt; @flight.seats } end </code></pre> <p>show.html.erb:</p> <pre><code>&lt;div id="seats"&gt; &lt;%= render :partial =&gt; 'seat_list', :locals =&gt; { :seats =&gt; @flight.seats } %&gt; &lt;/div&gt; &lt;%= link_to "Refresh Seats", "/flights/#{@flight.id}/seats", :remote =&gt; true %&gt; </code></pre> <p>Any idea why my <code>&lt;div id="seats"&gt;</code> won't refresh with the updated partial? I'm betting there is but i'll ask anyway, is something wrong with my code?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload