0

Devise を使用してアプリケーションにユーザーをログインさせる次の、かなり単純なコードがあります。これは、XHR ではなく HTML リクエストを使用して実行された場合、実際には問題なく機能します。こうするとXHRができて、

          = form_for(@user, :url => session_path(@user), :remote => true) do |f|
            = f.label :email
            = f.text_field :email, :size => 15, :maxlength => 32
            = f.label :password
            = f.password_field :password, :size => 15, :maxlength => 32
            %br
            = f.submit "Sign in"

結果は次のとおりです。

Started GET "/" for 127.0.0.1 at 2012-12-04 13:24:44 -0800
Processing by HomeController#index as HTML
  Rendered home/_hello_page.html.haml (0.1ms)
  Rendered home/index.html.haml within layouts/application (1.9ms)
Completed 200 OK in 18ms (Views: 16.3ms | ActiveRecord: 0.4ms)

Started POST "/users/sign_in" for 127.0.0.1 at 2012-12-04 13:24:54 -0800
Processing by Devise::SessionsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"1cGaycA20NMhK0fOwQyN8e3aSFwCHB6BZcLwmvKTI3U=", "user"=>{"email"=>"obscured@someplace.com", "password"=>"[FILTERED]"}, "commit"=>"Sign in"}
Completed 500 Internal Server Error in 65ms

ActionView::MissingTemplate (Missing template devise/sessions/create, devise/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
  * "/Users/sxross/Developer/iPhoneApps/motion/whos_here/whos_here_server/app/views"
  * "/Users/sxross/.rvm/gems/ruby-1.9.3-p327@whos_here_server/gems/devise-2.1.2/app/views"
):

すべてが私が期待したとおりでありProcessing by Devise::SessionsController#create as JS、受信コントローラーに「必要な JSON を返送してください」と伝えていることは明らかです。問題は、Devise コントローラーが JSON ではなくテンプレートをレンダリングしようとする理由と、それを修正するために何ができるかということです。

ありがとう!

4

1 に答える 1

1

views/devise/sessions に create.js.[erb|haml] ファイルがありません

ログインのためのリモートリクエストを送信すると、リクエストは Devise::SessionsController#create によって行われ、オーバーライドしない場合

respond_with resource, :location => after_sign_in_path_for(resource)

create.js.erb を探します。私は通常ちょうど持っています

window.location = "<%= root_url %>"

初期化。

また、設定する必要があります

config.http_authenticatable_on_xhr = false
config.navigational_formats = [:"*/*", "*/*", :html, :js]

これが機能するように config/initializers/devise.rb 内で。

私自身の場合、セッションコントローラーを次のようにオーバーライドしました。

@resource = warden.authenticate!(:scope => resource_name, :recall => "sessions#failure")
sign_in(resource_name, @resource)
respond_to do |format|
  format.html { respond_with @resource, :location => after_sign_in_path_for(@resource) }
  format.js
end

認証が失敗した場合は失敗メソッドにスローされ、それをfailure.js.erbで処理してユーザーに表示します

于 2013-02-21T10:01:06.600 に答える