问题描述
制定抛出:
"NoMethodError (undefined method `login' for #<ActionDispatch::Request:0x00000004e42d80>):
"
我每次尝试登录的时间。
在此应用登陆字段用作认证密钥:
every time I try to log in.In this application "login" field is used as authentication key:
/config/initializers/devise.rb:
/config/initializers/devise.rb:
config.authentication_keys = [ :login ]
在session_controller.rb我用的before_filter
:
In session_controller.rb I used before_filter
:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:user) { |u| u.permit(:login, :password) }
end
和我的的routes.rb
:
devise_for :users, :controllers => { :sessions => 'sessions', :registrations => 'registrations', :invitations => 'users/invitations'}
这个问题从Rails 3中升级到Rails的4后出现。
谁能向我解释,我在做什么错了?
This problem appeared after upgrade from Rails 3 to Rails 4.Can someone explain to me, what I'm doing wrong?
更新
我的坏。发现不对参数初始化色器件,由我的同事设置。
反正我有错误消息:
My bad. Found wrong parameter in devise initializer, set by my co-worker.Anyway i have error message:
NameError (undefined local variable or method `invitation_token' for #<User:0x0000000286c750>):
app/controllers/sessions_controller.rb:6:in `create'
会话#创建:
def create
self.resource = warden.authenticate!(auth_options)
sign_in(resource_name, resource)
render :json => { :user_id => resource.id }, :status => :created
end
更新
废话。我的同事也发生了变化database.yml,以另一个数据库。所以这个数据库没有被迁移到最后的状态=。耙DB后:迁移一切工作正常。感谢所有。
Crap. My co-worker also changed database.yml to another DB. So this DB was not migrated to last state =. After rake db:migrate all works fine. Thanks to all.
推荐答案
根据这个链接,你应该创建一个登录虚拟属性。
According to this link, you should create a login virtual attribute in the User model.
由任一用户名或电子邮件
结果验证#Virtual属性#这个是除了真正的坚持现场像用户名
结果 attr_accessor中:登录
#Virtual attribute for authenticating by either username or email
#This is in addition to a real persisted field like 'username'
attr_accessor :login
同时添加登录attr_accessible钢轨3
Also add login to attr_accessible for rails 3
attr_accessible :login
您可能还需要覆盖的用户模型设计的find_for_database_authentication方法
(假设它的ActiveRecord)
You may also need to overwrite Devise's find_for_database_authentication method in User model(assuming it is activerecord)
# app/models/user.rb
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
您可能需要修改配置/初始化/ devise.rb有
You may need to modify config/initializers/devise.rb to have
config.reset_password_keys = [ :login ]
config.confirmation_keys = [ :login ]
这篇关于导轨+设计sign_in错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!