2012-12-04 5 views
0

Devise를 사용하여 사용자를 내 애플리케이션에 로그인해야하는 다음과 같은 간단한 코드가 있습니다. 이것은 XHR이 아닌 HTML 요청을 사용하면 실제로 잘 작동합니다. 내가 이런 식으로 작업을 수행 할 때 XHR은Devits Ajax를 사용하여 로그인 할 때 401을 반환 할 때

  = 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 "https://stackoverflow.com/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"=>"[email protected]", "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/[email protected]_here_server/gems/devise-2.1.2/app/views" 
): 

모든 것이 정확하게하고 Processing by Devise::SessionsController#create as JS이 수신 컨트롤러를 말하고있는 것이 분명 내가 원한 JSON을 돌려 준다. " 그래서 문제는 Devise 컨트롤러가 JSON 대신 템플릿을 렌더링하려고하는 이유이며이를 수정하려면 어떻게해야합니까?

감사합니다.

+0

당신은 이것을 보았습니까? http://jessehowarth.com/devise –

+0

예. 나는 그랬다. 그 마법이 무엇인지는 잘 모르겠지만 jQuery.ajax를 적절한 accept-type과 함께 사용했을 때 200 반환 코드를 얻을 수있었습니다. 나는이 사실에 대한 진정한 "대답"이 없기 때문에 대답하지 않을 것이다. 그냥 쉬워야 할 것 같습니다. 그래도 고마워! –

답변

1

당신은 create.js 누락 [ERB | HAML]. 뷰에서 파일을// 세션

당신이 로그인에 대한 원격 요청을 보내 고안, 요청이 생성 고안 :: SessionsController 번호에 의해 수행되며, 그것을 덮어 쓰지 않으면

은 create.js.erb를 찾을 것입니다. 나는 ussualy 단지에서

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

있습니다.

또한, 당신은이 작업을 위해 설정/초기화/devise.rb 내부

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

을 설정해야합니다. 내 자신의 경우를

내가

이와 세션 컨트롤러를 재정의 : failure.js.erb에서

@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 
인증이 실패한 경우 실패 방법에 나를 던져

및 프로세스가 사용자를 보여 그

관련 문제