【问题标题】:Getting "undefined method `password' for #<User:0x2b95caea8590>" with Authlogic使用 Authlogic 获取“#<User:0x2b95caea8590> 的未定义方法‘密码’”
【发布时间】:2011-07-02 11:36:08
【问题描述】:

我意识到这个here 有一个几乎相同的问题,但那里的修复并没有解决我的问题。

我的 Authlogic 在我的开发环境中工作,但不在我的生产环境中。以下是我遇到的错误。

Started GET "/users/new" for 71.205.207.120 at Tue Feb 22 19:24:49 -0800 2011
  Processing by UsersController#new as HTML
Rendered users/_form.html.erb (19.3ms)
Rendered users/new.html.erb within layouts/application (21.6ms)
Completed   in 38ms

ActionView::Template::Error (undefined method `password' for #<User:0x2b95caea8590>):
    21:   </div>
    22:   <div class="field">
    23:     <%= f.label :password %><br />
    24:     <%= f.password_field :password %>
    25:   </div>
    26:   <div class="field">
    27:     <%= f.label :password_confirmation %><br />
  app/views/users/_form.html.erb:24:in `_app_views_users__form_html_erb___1397135998_23961177217120_1124190'
  app/views/users/_form.html.erb:1:in `_app_views_users__form_html_erb___1397135998_23961177217120_1124190'
  app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___1081160896_23961177226000_0'
  app/controllers/users_controller.rb:29:in `new'

我没有拼错任何数据库列,我已经在做acts_as_authentic 并且我已经重新启动了我的服务器,所以我不确定还有什么可以尝试的。看来这一定与我的数据库有关,但我的数据库结构似乎是相同的。

【问题讨论】:

  • 你迁移生产服务器上的数据库了吗?
  • 是的,我确认我的数据库更改已显示。
  • 重启生产服务器怎么样?不是操作系统服务器,只是 rails 服务。
  • 尝试在生产服务器上运行 Rails 控制台并运行以下命令:User.new.methods.grep /^password/ 是否返回 :password 方法?
  • @CalebHC:不,它没有。在开发中可以,但在生产中不会。对下一步做什么有什么建议吗?

标签: ruby-on-rails ruby-on-rails-3 authentication passwords authlogic


【解决方案1】:

数据库密码字段(和/或盐字段)可能与任何默认值都不匹配!!!

我现在遇到过两次这个问题,在这两种情况下都很棘手,因为有两件事:

1) 消息“未定义的方法‘密码’实际上(通常)表示使用默认值在数据库中找不到密码字段。

2) 添加数据库字段以获取密码(或盐)的位置不在应用程序目录中的任何位置,而是在您的 ruby​​ gem 目录中,例如 ~/.gem/ruby/1.8/gems/authlogic -2.1.6/lib/authlogic/acts_as_authentic !

要更改的字段是密码.rb。将您的字段名称添加到列表中,例如我添加了 pd_hashed 更改:

模块配置 # 数据库中 crypted_pa​​ssword 字段的名称。 # # * 默认值::crypted_pa​​ssword、:encrypted_pa​​ssword、:password_hash 或 :pw_hash # * 接受:符号 def crypted_pa​​ssword_field(value = nil) rw_config(:crypted_pa​​ssword_field, value, first_column_to_exist(nil, :pd_hashed, :crypted_pa​​ssword, :encrypted_pa​​ssword, :password_hash, :pw_hash)) 结束

对盐场做同样的事情。

重新启动,这可能会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多