English 中文(简体)
Rails 3.2.x remote=>true仍然重新加载页面
原标题:Rails 3.2.x remote=>true still reloads a page

我一直在到处找,不明白为什么这不起作用。

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

The problem is I d get to the commit method, but the page would reload, which defeats the point of remote=>true Also the commit.js never seemed to get called.

Note 1: If I exclude this line, I get the blank page to /commit. Including it makes the page just reload
Note 2: I ve tried both of these approaches suggested by other SO posts
Note 3: I ve tried both using link_to and 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 %>


The same with the form:

让您:

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

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

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

问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签