新安装的Rails + Devise总是获得401 Unauthorized

koo*_*osa 13 ruby ruby-on-rails devise ruby-on-rails-3

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

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

我的用户型号:

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
Run Code Online (Sandbox Code Playgroud)

我的日志:

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
Run Code Online (Sandbox Code Playgroud)

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

koo*_*osa 26

沮丧的这个小练习在RTFM中是一个很好的教训.我用可确认的方式设置了Devise,当我创建布局时,我忽略了插入以下行:

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
Run Code Online (Sandbox Code Playgroud)

......因为它明确指出要在入门指南中做.当我插入这些时,我收到错误消息"您需要在登录前确认您的电子邮件地址."

在控制台中,我为用户设置了confirmed_at = Time.now,瞧,我现在可以登录了.

  • 对于某些用户,请尝试使用@@ user.skip_confirmation!。您不会允许他/她获取确认电子邮件。 (3认同)