【发布时间】:2014-01-02 08:22:34
【问题描述】:
我正在尝试在生产中启动我的 Rails 4 应用程序(它只是一个简单的博客,其中包含一个登录表单,供数据库中的用户登录和 CRUD)
我正在运行带有 unicorn 和 bcrypt 的 nginx,以在 User 模型上使用 has_secure_password 进行用户身份验证。当我尝试登录用户时,我的 production.log 显示:
NoMethodError (undefined method `authenticate' for nil:NilClass):
app/controllers/sessions_controller.rb:9:in `create'
在我的 Sessions 控制器中,这个块是调用验证方法的部分:
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to root_path
else
flash.now[:warning] = "Invalid User/Password combination"
render 'new'
end
除非我将 else 语句更改为其他内容,否则我会收到“无效的用户/密码组合”错误消息并呈现 /sessions
真正奇怪的是,当我在 Rails 控制台中运行 User.find_by_email("user's email").authenticate("user's password") 时,我得到了用户返回给我。
我完全不明白为什么我的用户身份验证在开发模式下有效,但在生产模式下无效!
以下是遇到相同问题的示例应用程序的 github 存储库: https://github.com/sentrathis96/debug_app
【问题讨论】:
标签: ruby-on-rails nginx unicorn bcrypt