我正在使用Rails 3.0.3和jQuery ujs。無法獲得FORM在Rails 3.0.3中遠程提交?
我想遠程發送表單。包含表單視圖模板電源/ index.html.erb看起來如下:
...
<%= form_tag :remote => true, :controller => "mains", :action => "search", :format => :js do %>
<div class="dialog">
<table>
<tbody>
<tr class="prop">
<td valign="top" class="name">
<%= label_tag(:location_start, "Location Start:") %>
</td>
<td valign="top">
<%= text_field_tag(:location_start) %>
</td>
...
</tbody>
</table>
</div>
<div class="buttons">
<span class="button"><%= submit_tag("Search") %></span>
</div>
<% end %>
...
控制器mains_controller.rb如下所示(去除不需要的東西):
class MainsController < ApplicationController
def index
respond_to do |format|
format.html # index.html.erb
end
end
def search
respond_to do |format|
format.js { render :template => 'mains/search.js.erb'}
end
end
end
的查看模板電源/ search.js.erb具有以下內容:
$('#search').html("<h1>Headline</h1>");
由於使用Rails(4h版)進行敏捷Web開發的章節「26.4使用JQuery編寫更少和做得更多」中的說明,我正在使用search.js.erb而不是search.js.rjs。我還添加了:format =>:js歸因於https://github.com/rails/jquery-ujs/issues/closed/#issue/52。
當我打電話http://0.0.0.0:3000/mains/index並提交表單,它實際上不是遠程提交,但瀏覽器加載http://0.0.0.0:3000/mains/改爲search.js?remote = true。瀏覽器輸出$('#search').html("<h1>Headline</h1>");
,而不是通過用給定的<h1>Headline</h1>
代替div#搜索來執行JavaScript。
我試圖找到一個解決方案,但到目前爲止沒有成功......任何人都可以幫忙嗎?
感謝您提供其他人的解決方案! – 2013-05-17 17:35:23