RoR ajax not updating page with page.replace_html
I am using the standard ajax libraries with Rails. I know the event is happening because if I refresh the page the element updates fine. Here is my view code where I initiate the ajax.
<% form_tag('switch_car', :method => :put, :remote => true) do %>
<div class="field">
<label>Car Name:</label>
<%= select_tag(:id, options_from_collection_for_select(active_cars, "id", "name"))%><%= submit_tag "Switch Car" %>
</div>
<% end %>
here is my update.js.rjs
page.replace_html('entries', render(@miles))
here is the section of the view that should update.
<div id="entries">
<%= render(@miles)%>
</div>
Here is my controller code.
def update
respond_to do |format|
if params[:id]
session[:car_info_id] = params[:id]
format.html {redirect_to entry_url}
format.js
else
redirect_to switch_car_path
end
end
end
I am not sure why it is not modifying the DOM so the update shows up without refreshing the page? I can see in the server console that it runs the javascript but the view does not update.
Console output:
Started POST "/switch_car" for 127.0.0.1 at 2011-08-03 17:32:02 -0400
Processing by ActiveCarController#update as JS
Parameters: {"utf8"=>开发者_运维问答"Γ£ô", "authenticity_token"=>"PEbdqAoiik37lcoP4+v+dakpYxdpMkSm7Ub8eZpdF9I=", "id"=>"10", "commit"=>"Switch Car", "_"
=>""}
Rendered active_car/update.html.erb (0.0ms)
Completed 200 OK in 16ms (Views: 16.0ms | ActiveRecord: 0.0ms)
The issue all along was I had my update.js.rjs in the wrong directory. You need to have it in the view directory where the action happens not in the view directory that provides the data. What finally made me catch on is that the update.js.rjs was never rendered see above terminal output. It says renered update.html.erb because it could not find the update.js.rjs. So if anybody else has this problem make sure your update.js.rjs is getting rendered.
精彩评论