4

私はずっと検索してきましたが、なぜこれが機能しないのかわかりません。

非常に基本的な 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の両方を使用して試しました

誰でも助けることができますか?ありがとう!

4

1 に答える 1

4

なんで2行入れたの?

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

2枚目は外して!

交換:

<%= 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

于 2012-05-10T12:08:18.950 に答える