Rails3でajaxyサインアップを実装しようとしています。jquery-ujsとリモートフォームを使用しています。リクエストでサインアップフォームにアクセスすると$.get
、正しく表示されます。このサインアップフォームはリモートです:
form_for @user, :remote => true, :html => {"data-type" => "html"} do |f|
私のapplication.jsでは、フォームを取得するためのajaxリクエストが成功した場合、控えめなJavaScriptからajaxイベントのハンドラーをバインドしようとしています。
var load_remote_form = function(evt) {
var href = $(this).attr('href');
// load form template
$.get(href, {}, function(data) {
$('body').append(data);
$('form[data-remote]').bind("ajax:beforeSend", function(e) {
console.log("Caught beforeSend!");
});
});
evt.preventDefault();
};
$(document).ready(function() {
$('a#signup').click(load_remote_form);
});
Chromeの開発ツールは、イベント " ajax:beforeSend
"がバインドされていることを示していますが、処理されません(フォームを送信するとき、javascriptコンソールには何もありませんが、Railsログでは、要求が正しく処理され、応答が送信されます)。のように、他のイベントを同じセレクター(form[data-remote]
)にバインドできclick
、それらは正しく処理されます。
バインドされたAjaxイベント.live
も処理されません。ただし、フォームがレイアウトの一部としてレンダリングされる場合(つまり、 http:// localhost:3000 / signup /のようなスタンドアロンページにある場合)、バインドされた" ajax:beforeSend
"は正しく処理されます。
私のコントローラー(念のために、それはひどく書かれているかもしれませんが、私はそれがうまくいくと確信しています):
class UsersController < ApplicationController
layout :layout_for_type
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
flash[:notice] = "You have successfully registered."
redirect_to root_url
else
if request.xhr?
render :action => 'new', :status => :unprocessable_entry, :layout => false
else
render :action => 'new'
end
end
end
def layout_for_type
request.xhr? ? nil : "application"
end
end
私は何が間違っているのですか?そのような「ダブルアジャクス」フォームを実装するためのより良いアプローチがあるでしょうか?