更新頁面,我使用它與Rails的標準的AJAX庫。我知道事件正在發生,因爲如果我刷新頁面元素更新正常。這裏是我啓動ajax的視圖代碼。回報率AJAX不是page.replace_html
<% 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 %>
這裏是我的update.js.rjs
page.replace_html('entries', render(@miles))
這裏是應該更新視圖的部分。
<div id="entries">
<%= render(@miles)%>
</div>
這是我的控制器代碼。
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
我不確定爲什麼它不修改DOM,因此更新顯示沒有刷新頁面?我可以在服務器控制檯中看到它運行的是JavaScript,但視圖不更新。
控制檯輸出:
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)
它不呈現update.js.rjs'渲染active_car/update.html.erb(0.0ms)'。請求格式是html(不是js):''開始POST'/ switch_car'' – Hck