加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 02:31:56 所属栏目:百科 来源:网络整理
导读:我一直在搜索,无法弄清楚为什么这不起作用. 我正在尝试测试一个非常基本的ajax动作.这是我的代码: 控制器: def commit respond_to do |format| format.html { redirect_to :action = "index" } # see note 1 format.js { render :layout = false } # see n
我一直在搜索,无法弄清楚为什么这不起作用.

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

commit.js.erb

console.log('committed');
$('#message').html("committed");

问题是我得到了提交方法,但页面会重新加载,这会使remote => true失败
也似乎没有调用commit.js.

注1:如果我排除了这一行,我将空白页面提交给/ commit.包括它使页面只是重新加载
注2:我尝试过其他SO帖子提出的这两种方法
注3:我尝试过使用link_to和form_tag

有人可以帮忙吗?谢谢!

解决方法

你为什么要放2条线?

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

删除第二个!

更换:

<%= link_to "commit",:remote => true %>

有:

<%= link_to "commit",commit_path,:remote => true %>

形式相同:

做你的:

<%= form_tag( :action => "commit",:method => :post) do %>

如:

<%= form_tag(commit_path,:remote => true) do %>

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读