Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 设计在使用Ajax登录时返回401_Ruby On Rails_Devise_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 设计在使用Ajax登录时返回401

Ruby on rails 设计在使用Ajax登录时返回401,ruby-on-rails,devise,ruby-on-rails-3.2,Ruby On Rails,Devise,Ruby On Rails 3.2,我有以下代码,非常简单,应该使用Desive将用户登录到我的应用程序中。如果使用HTML请求而不是XHR来完成,那么这实际上可以很好地工作。当我这样做的时候,XHR就产生了 = form_for(@user, :url => session_path(@user), :remote => true) do |f| = f.label :email = f.text_field :email, :size =>

我有以下代码,非常简单,应该使用Desive将用户登录到我的应用程序中。如果使用HTML请求而不是XHR来完成,那么这实际上可以很好地工作。当我这样做的时候,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"
):
一切都完全符合我的预期,很明显,Deave::SessionController#create as JS的
处理过程告诉接收控制器“将我想要的JSON寄回”。所以问题是,为什么Deave控制器试图呈现一个模板而不是JSON,我能做些什么来修复它


谢谢

视图/设计/会话中缺少create.js.[erb | haml]文件

当您发送远程登录请求时,该请求由designe::sessioncontroller#create完成,如果您没有覆盖它

respond_with resource, :location => after_sign_in_path_for(resource)
将查找create.js.erb。 我通常只有

window.location = "<%= root_url %>"
在config/initializers/develope.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中对其进行处理,以向用户显示

您是否尝试过查看此信息?是 啊是的。我不知道有什么神奇之处,但当我使用jQuery.ajax和正确的accept类型以及all时,我能够得到我的200返回代码。我不会回答这个问题,因为我没有真正的“答案”。看起来应该更容易些。不过,谢谢你的推荐!
@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