#<用户 ID: nil, created_at: nil, updated_at: nil> 的未定义方法"email"



无法弄清楚这里出了什么问题。按照Devise的设置说明,在谷歌上搜索了我能想到的一切,仍然没有运气。

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

我已经运行了rakedb:migrate,重置服务器,你有什么。我还是不知道哪里出了问题。我甚至有另一个基本的应用程序也有同样的设置,仔细查看源代码,似乎我做得都对了,只是看不出问题。

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

我猜你用了Devise<2.0版本,现在您使用的是最新版本。

自2.0以来,Devise不再自动将列包含在您的模型中,请参阅此页面了解更多信息。

很明显,users表中没有username列。所以首先创建这个。

rails g migration add_username_to_users用户名:字符串:uniq

然后运行CCD_ 4。现在您有一个username列,但它不允许作为强参数,所以在应用程序控制器中包括以下行。

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

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

中的属性访问器

最新更新