我有一个用户模型和从零开始构建的身份验证系统的简单应用程序。我现在试图使用Devise来代替它,它不起作用,并且作为web开发中的新手,有一些我没有掌握,我不知道如何调试。设计:登录失败,如何调试?
我的问题是:设计安装和工作,除非登录始终返回“无效的电子邮件或密码”,即使这两个字段都是正确的。
我开发了一个空应用程序,添加了Devise,并且我没有这个问题。因此,问题可能是因为我试图将Devise添加到现有的用户模型。
我已阅读doc和Devise wiki,其中有一个关于此主题的主题:here。说从迁移字段中删除:database_authenticatable,因为用户模型已经有一个电子邮件字段,并用t.encrypted_password替换它,这是我在迁移中所做的。
现在,在我的用户模型中,我离开了:attr_accessible中的database_authenticatable。如果我删除它,我有很多错误消息,session_path无法识别,等等......但它没有被迁移?另外,:encrypted_password没有出现在我的模型中的任何地方,这是正常的吗?...
我知道这真的是一个新手问题,我有点失落,不知道是否应该从我的应用程序重写开始或者是否有简单的解决方法......我也不知道如何调试,我在日志中看到的所有内容都是当用户应该成功登录时出现“未授权”,并且“authentication_token”不是试图sign_in了一个生成一次签署了
所以当同样的,我迷路了,如果它似乎很明显你的,我会很高兴听到任何意见...
我下面添加routes.rb,User.rb,schema.rb和迁移文件
routes.rb中:
TapaG::Application.routes.draw do
devise_for :users
get "pages/home"
resources :users
resources :belongings
devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
get "sign_out", :to => "devise/sessions#destroy"
get "sign_up", :to => "devise/registrations#new"
end
get "pages/more_details"
get "pages/infos_pratiques"
get "pages/contact_us"
#match 'Profil', :to => 'users#Profil'
match "more_details", :to => "pages#more_details"
match 'contact_us', :to => 'pages#contact_us'
match "infos_pratiques", :to => "pages#infos_pratiques"
match '/belongings/new', :to => 'belongings#new'
root :to => 'pages#home'
迁移:
class AddDeviseToUsers < ActiveRecord::Migration
def self.up
change_table(:users) do |t|
t.recoverable
t.rememberable
t.trackable
t.encrypted_password :null => false, :default => '', :limit => 128
# t.encryptable
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
# Uncomment below if timestamps were not included in your original model.
# t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end
User.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :registerable, #:database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessor :password
attr_accessible :name, :number_of_positive_reco, :confidence_percent, :avatar
schema.rb:
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.integer "number_of_positive_reco"
t.float "confidence_percent"
t.datetime "created_at"
t.datetime "updated_at"
t.string "encrypted_password"
t.string "salt"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.boolean "admin", :default => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
实际上,Devise不会做任何密码加密(用户注册后加密密码和salt都设置为NIL)... – citraL 2012-02-16 13:29:01
啊,现在明白了,谢谢你的解释:) – socjopata 2012-02-16 21:43:05