undefined method 'devise' for User

I ran into a similar issue when I was configuring Devise (Ruby 2.4.1 / Rails 5.1.2). In my case it seems that the following files were not created after I executed: rails generate devise:install for the first time.

create  config/initializers/devise.rb
create  config/locales/devise.en.yml

Steps that I followed:

1) Comment from your MODEL the following:

  #devise :database_authenticatable, :registerable,
     #:recoverable, :rememberable, :trackable, :validatable

2) Comment from routes.rb:

#devise_for :sessions

3) Run rails generate devise:install again, you should see that some files are created this time. Hope you it works !

4) Uncomment from 1) & 2)

5) Execute: rake db:migrate

And at this point it should work. Hope it helps someone !


Add devise to your application Gemfile and install it by running bundle install. After this, you should run the following generator command:

rails generate devise:install

This generator will install an initializer your_application/config/initializers/devise.rb which consists of all the Devise's configuration options.

You missed the above mentioned step which is why the devise configurations are not set and you receive undefined method 'devise' for User error in your model class User.