新安装的 Rails + Devise 总是得到 401 Unauthorized [英] Always getting 401 Unauthorized with new install of Rails + Devise
问题描述
我安装了新的 Rails,正在尝试使用 Devise 设置身份验证.据我所知,我有一个非常基本的设置应该可以工作,但是每当我尝试使用默认的 Devise 登录表单登录时,我都会收到未经授权的错误.我确定我的凭据是正确的,因为我在控制台中创建了一个用户进行测试,如下所示:
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
我的用户模型:
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
我的日志:
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
有什么方法可以让我从 Devise 中获得更多关于失败的信息?当我在控制台中创建用户时,使用的加密与通过表单使用的加密不同吗?
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?
推荐答案
这个令人沮丧的小练习被证明是 RTFM 的一个很好的教训.我已经用可确认设置了设计,当我创建布局时,我忽略了插入以下几行:
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."
在控制台中,我为用户设置了 Confirmed_at = Time.now,瞧,我现在可以登录了.
In the console I set confirmed_at = Time.now for the User, and voilà, I can now log in.
这篇关于新安装的 Rails + Devise 总是得到 401 Unauthorized的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!