JQuery が link_to メソッドに対して 2 つの AJAX リクエストを作成しているという奇妙な問題があります。UJS 用の JQuery を使用して Rails 3 アプリを開発しています。「フォロー」と「フォロー解除」を切り替えるトグル リンクがあります。
私のリンクは次のようにレンダリングされます。
<span id="follow_link">
<a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">Unfollow</a>
</span>
私のコントローラーは次のようにセットアップされています:
def tfollow_artist
@artist = Artist.find(params[:id])
if current_user.following?(@artist)
current_user.stop_following(@artist)
else
current_user.follow(@artist)
end
end
最終的に js を次のようにレンダリングします。
$('#follow_link').html('<%= escape_javascript(render :partial => "follow") %>');
これは本質的に、'<span id="follow_link">...</span> の html コンテンツを同じ URL に置き換え、テキストが異なるだけです。たとえば、上記は次のようにレンダリングされます。
<span id="follow_link">
<a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">Follow</a>
</span>
ただし、これにより、何らかの理由で JQuery が 2 つの AJAX リクエストを作成することになります。
ここで何が悪いのか誰にもわかりますか?
最新のjquery-ujsファイルをアプリにコピーする「jquery-rails」gemを使用しています。JQuery のバージョンは 1.4.3 です