Undefined email method for # <User id: nil, created_at: nil, updated_at: nil>

I can’t understand what’s wrong here. The following setup instructions, googled everything I can come up with is still out of 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 is 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 launched rake db: migrate, reset the server that you have. Still can't figure out where I'm wrong. I even have another basic application with the same setup, combing the source, it looks like I did everything right, I just do not see the problem.

+4
source share
3 answers

Basically, your error means that your user model does not have an email column (or attr_accessor).

I assume you used Devise <2.0 and now you are using the latest version.

Starting with version 2.0, Devise no longer includes columns in your model, see this page for more information .

+3
source

Obviously, there is no username column in the users table. First create it.

rails g migration add_username_to_users username: string: uniq

then run rake db:migrate . Now you have a username column, but it is not allowed as strong parameters, so include the following line in the 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 
+1
source

specify the email column as attr_accessor in the user model

0
source

Source: https://habr.com/ru/post/1434407/


All Articles