gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:04:07 24 4
gpt4 key购买 nike

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

我正在尝试测试一个非常基本的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com