总是通过新的Rails + Devise安装获得401未经授权 [英] Always getting 401 Unauthorized with new install of Rails + Devise

查看:113
本文介绍了总是通过新的Rails + Devise安装获得401未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新的安装Rails,并且正在尝试使用Devise设置身份验证。据我所知,我有一个非常基本的设置应该工作,但是每当我尝试使用默认的Devise登录形式登录时,我会收到一个未经授权的错误。我确定我的凭证是正确的,因为我创建了一个用户在控制台中进行测试,如下所示:

  User.new({ :email =>'mark@markdavies.com.au',:priv_level =>'admin',:password =>'mypassword',:password_confirmation =>'mypassword'})save 

我的用户模型:

  class User< ActiveRecord :: Base 

#包含默认设计模块。其他可用的是:
#:token_authenticatable,:confirmable,
#:lockable,:timeoutable and:omniauthable
devise:database_authenticatable,:可恢复,可记忆,可追溯,可验证,可确认

#模型的可访问(或受保护)属性
attr_accessible:email,:password,:password_confirmation,:remember_me,:priv_level,:unconfirmed_email
#attr_accessible:title, :body

has_one:supplier

end

我的日志:

 在2012-12-22 13:12启动POST/ admin / user / sign_in为127.0.0.1 13:参数:{utf8=>✓,authenticity_token=>wYLsalxN9rTv8P8bvYuT0wZcvlFbu6b1SvoCyKtTCII =,admin_user => {email=>mark@markdavies.com.au,password=>[FILTERED],remember_me=>0},commit=&登录} 
用户加载(0.2ms)SELECTusers。* FROMusers email='mark@markdavies.com.au'LIMIT 1
(0.1ms)开始交易
(0.0ms)提交交易
完成401未经授权在69ms

有没有什么方法可以获得有关Devise失败的更多信息?当我在控制台中创建用户时,加密使用的不同于通过表单?

解决方案

在RTFM中成为一个很好的教训。我已经设置了可以确定的Devise,当我创建了我的布局时,我忽略了插入以下几行:

 < p class =notice><%= notice%>< / p> 
< p class =alert><%= alert%>< / p>

...正如在入门指南中所说明的那样。当我插入这些我收到错误信息你需要确认您的电子邮件地址登录之前。



在控制台我设置了confirm_at = Time.now为用户,和voilà,我现在可以登录。


I have a new install of Rails and am trying to set up authentication with Devise. As far as I can tell I have a very basic set up that should work, but whenever I try to log in with the default Devise sign in form I get an Unauthorized error. I am sure my credentials are correct as I created a User to test with in the console like so:

User.new({:email=>'mark@markdavies.com.au', :priv_level => 'admin', :password=>'mypassword', :password_confirmation=>'mypassword'}).save

My User model:

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :confirmable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :priv_level, :unconfirmed_email
  # attr_accessible :title, :body

  has_one :supplier

end

My log:

Started POST "/admin/user/sign_in" for 127.0.0.1 at 2012-12-22 13:10:56 -0500
Processing by Admin::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wYLsalxN9rTv8P8bvYuT0wZcvlFbu6b1SvoCyKtTCII=", "admin_user"=>{"email"=>"mark@markdavies.com.au", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'mark@markdavies.com.au' LIMIT 1
(0.1ms)  begin transaction
(0.0ms)  commit transaction
Completed 401 Unauthorized in 69ms

Is there any way I can get more information about what is failing from Devise? When I create the user in the console is the encryption used different than through the forms?

解决方案

Well this little exercise in frustration turned out to be a good lesson in RTFM. I had set up Devise with confirmable, and when I created my layouts I neglected to insert the following lines:

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

... as it clearly states to do in the getting started guide. When I inserted these I got the error message "You need to confirm your email address before logging in."

In the console I set confirmed_at = Time.now for the User, and voilà, I can now log in.

这篇关于总是通过新的Rails + Devise安装获得401未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆