ruby-on-rails - Rails 3.2.x remote=>true 仍然重新加载页面

标签 ruby-on-rails ajax

我一直在搜索,无法弄清楚为什么这不起作用。

我正在尝试测试一个非常基本的 ajax 操作。这是我的代码:

Controller :

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

看法:
<%= link_to "commit", :action => "commit", :remote => true %>
<%= form_tag( :action => "commit", :remote => true, :method => :post) do %>
     <%= submit_tag "commit" %>
<% end %>

<div id='message'></div>

提交.js.erb
console.log('committed');
$('#message').html("committed"); 

问题是我会使用 commit 方法,但是页面会重新加载,这违背了 remote=>true 的点
此外,commit.js 似乎从未被调用过。

注意 1:如果我排除这一行,我会得到空白页到/commit。包括它会使页面重新加载
注 2:我已经尝试了其他 SO 帖子建议的这两种方法
注 3:我已经尝试使用 link_to 和 form_tag

任何人都可以帮忙吗?谢谢!

最佳答案

为什么要放两行?

    format.js { render :layout => false } # see note 2
    format.js { render :nothing => true } 

去掉第二个!

代替:
<%= 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 %>

注:POST是默认行为,您可以从 form_tag 中省略它.

关于ruby-on-rails - Rails 3.2.x remote=>true 仍然重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10532169/

相关文章:

javascript - 在 Rails 中动态选择项目

ruby-on-rails - 不同用户的 Action Controller Live

mysql - 请求卡在 ActiveRecord::QueryCache 中间件中

新附加 HTML 上的 Javascript window.print() 问题

Java Servlet - Ajax 请求返回混合响应

ruby-on-rails - 用户中的 NoMethodError #index 'undefined method ` each' for nil :NilClass'

ruby-on-rails - fields_for 表单生成器对象为零

javascript - 如何将模板数据导入Ajax版Smart Admin主题?

javascript - 重新创建 Ajax POST

javascript - 无法在 AJAX 回调中访问全局数组