Ruby on rails Rails 3.2.x remote=>;true仍然会重新加载页面
我一直在到处寻找,不知道为什么这不起作用 我试图测试一个非常基本的ajax操作。这是我的代码: 控制器:Ruby on rails Rails 3.2.x remote=>;true仍然会重新加载页面,ruby-on-rails,ajax,Ruby On Rails,Ajax,我一直在到处寻找,不知道为什么这不起作用 我试图测试一个非常基本的ajax操作。这是我的代码: 控制器: def commit respond_to do |format| format.html { redirect_to :action => "index" } # see note 1 format.js { render :layout => false } # see note 2 format.js { render
def commit
respond_to do |format|
format.html { redirect_to :action => "index" } # see note 1
format.js { render :layout => false } # see note 2
format.js { render :nothing => true }
end
end
视图:
问题是我会使用commit方法,但是页面会重新加载,这会破坏remote=>true的观点
而且commit.js似乎从未被调用过
注1:如果我排除这一行,我会得到空白页到/提交。包括它会使页面重新加载注2:我已经尝试了其他SO帖子建议的这两种方法
注3:我尝试过使用link_to和form_标记
有人能帮忙吗?谢谢 为什么要放两行
format.js { render :layout => false } # see note 2
format.js { render :nothing => true }
移除第二个
替换:
<%= link_to "commit", :action => "commit", :remote => true %>
“提交”,:remote=>true%>
与:
true%>
与表格相同: 制作您的:
<%= form_tag( :action => "commit", :remote => true, :method => :post) do %>
“提交”,:remote=>true,:method=>:post)do%>
作为:
true)do%>
注意:
POST
是默认行为,您可以从form_tag
中省略它!结果表明,format.js参数中的任何一个都有效,但我只需要传递路径,而不是:action!谢谢这样看::action
定义路径,:remote
定义其处理方式。把它们放在一起很奇怪。如果希望在参数中传递:remote
,该怎么办?那将是一片混乱!
<%= link_to "commit", :action => "commit", :remote => true %>
<%= link_to "commit", commit_path, :remote => true %>
<%= form_tag( :action => "commit", :remote => true, :method => :post) do %>
<%= form_tag(commit_path, :remote => true) do %>