未定义的方法`email'for#<user id:=“”nil,=“”created_at:=“”nil,=“”updated_at:=“”nil =“”> </user>

时间:2012-09-15 21:19:22

标签: ruby-on-rails ruby-on-rails-3 devise

无法弄清楚这里有什么问题。跟着Devise设置说明,用Google搜索我能想到的一切,但仍然没有运气。

undefined method `email' for #<User id: nil, created_at: nil, updated_at: nil>
Extracted source (around line #7):

4:   <%= devise_error_messages! %>
5: 
6:   <div><%= f.label :email %><br />
7:   <%= f.email_field :email %></div>
8: 
9:   <div><%= f.label :password %><br />
10:   <%= f.password_field :password %></div>

这是我的用户模型:

class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  validates_presence_of :email
  validates_uniqueness_of :email, :case_sensitive => false

end

我运行rake db:migrate,重置服务器,你有什么。仍然无法弄清楚我哪里出错了。我甚至有另一个基本应用程序使用相同的设置,梳理源代码似乎我已经完成了所有事情,只是看不出问题。

3 个答案:

答案 0 :(得分:3)

基本上,您的错误意味着您的用户模型中没有email列(或attr_accessor)。

我猜你用的是Devise&lt; 2.0现在使用的是最新版本。

自2.0以来,Devise不再自动将列添加到您的模型中see this page for more info

答案 1 :(得分:1)

显然,username表中没有users列。首先创建此列。

  

rails g migration add_username_to_users username:string:uniq

然后运行rake db:migrate。现在您有一个用户名列,但不允许它作为强参数,因此在Application controller中包含以下行。

class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
 added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
 devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
 devise_parameter_sanitizer.permit :account_update, keys: added_attrs
 end
end

答案 2 :(得分:0)

提及电子邮件列作为用户模型中的attr_accessor

相关问题