Reputation: 105
Can't figure out what's wrong here. Followed Devise setup instructions, Googled everything I can think of, still no luck.
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>
Here's my user model:
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
I've run rake db:migrate, reset the server, what have you. Still can't figure out where I'm going wrong. I've even got another basic app with the same setup, combing through the source it seems like I've done everything correctly, just can't see the problem.
Upvotes: 4
Views: 9420
Reputation: 1094
Clearly you have not username
column in users
table.So first create this.
rails g migration add_username_to_users username:string:uniq
then run rake db:migrate
. Now you have a username column but it has not be permitted as strong parameters so include following line in 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
Upvotes: 1
Reputation: 115541
Basically your error means you don't have an email
column (or attr_accessor) in your User model.
I guess you used Devise < 2.0 and now you use the latest version.
Since 2.0, Devise doesn't include the columns automatically to your model anymore, see this page for more info.
Upvotes: 3