undefined method 'devise' for User

Ruby on-RailsRubyDevise

Ruby on-Rails Problem Overview


I have been looking to get to grips with devise and its workings and have kind of fallen at the first hurdle. I have looked in a few places but cannot seem to find someone with this error exactly.

So I have created a simple Home controller with an index view and added root 'home#index' and also ensured the default url options are setup in the development.rb file. I then simply typed:

rails generate devise User

This created my user.rb file in models with the following:

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

Pretty straightforward so far, I have the following Gemfile:

source 'https://rubygems.org'
gem 'rails', '4.0.5'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.2'
gem 'devise'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
  gem 'sdoc', require: false
end
gem 'bcrypt'

And when I run either rake db:migrate I get the following error:

rake aborted!
NoMethodError: undefined method `devise' for User (call 'User.connection' to establish a connection):Class
/home/jonlee/.rvm/gems/ruby-2.1.1@railstutorial_rails_4_0/gems/activerecord-4.0.5/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/home/jonlee/Projects/rails/userauth/app/models/user.rb:4:in `<class:User>'
/home/jonlee/Projects/rails/userauth/app/models/user.rb:1:in `<top (required)>'

Im at a loss as to why the User model cannot find the 'devise' method when as far as I can see it is definitely there.

I get similar errors with rake routes, rails server and rails console.

For further info I am using ruby 2.1.1 if that helps?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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 !

Solution 3 - Ruby on-Rails

I have the same issue but with other reason. this can also be problem for somebody. Stop rails server and then type

 rails s

to restart it

Solution 4 - Ruby on-Rails

I've run the generator $ rails generate devise:install but got the same issue.

Anyway it works to me: Add extend Devise::Models to the User models.

Solution 5 - Ruby on-Rails

In addition to Kirti's answer of running

rails generate devise:install

you may want to rename the devise initializer to

config/initializers/01_devise.rb

because if any other initializer (such as config/initializers/active_admin.rb) runs before devise and touches the ApplicationController, you will get the same error.

And according to http://guides.rubyonrails.org/configuring.html#using-initializer-files you control the load order of initializers using file naming.

Solution 6 - Ruby on-Rails

I had the same issue. The fix was to uncomment:

require 'devise/orm/active_record'

in config/initializers/active_admin.rb.

I deliberately commented it out earlier because documentation says: "Supports :active_record (default)".

Solution 7 - Ruby on-Rails

So I just had this same error crop up when I did:

rails g controller client/timesheets

It turned out that the problem was with the generated helper:

app/helpers/client/timesheet_helper.rb:

module Client::TimesheetHelper
end

Client is a model that uses Single Table Inheritance off of the User model. Something was getting very mixed up her, what with Client being a Model (class) and a module, and it results in this cryptic error message.

YMMV.

Solution 8 - Ruby on-Rails

I got the same error when I installed Devise and Mongoid, but forgot to change the orm from active_record to mongoid in config/initializers/devise.rb

Should be:

require 'devise/orm/mongoid'

Solution 9 - Ruby on-Rails

I had to stop the Spring preloader process:

spring stop

Now works as expected

Solution 10 - Ruby on-Rails

I had this problem because executed rails g devise user before to rails g devise:install

I solved this, removing line devise_for :users at config/routes.rb and deleting db/migrate/xxxxxx_add_devise_to_users.rb

now run

rails g devise:install

rails g devise user

Solution 11 - Ruby on-Rails

Extend Devise::Model

class User < ActiveRecord::Base
  extend Devise::Models
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  include DeviseTokenAuth::Concerns::User
end

Reference: https://dev.to/risafj/guide-to-devisetokenauth-simple-authentication-in-rails-api-pfj

Solution 12 - Ruby on-Rails

Comment out:-

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

in user.rb Model. This worked for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonleePeakmanView Question on Stackoverflow
Solution 1 - Ruby on-RailsKirti ThoratView Answer on Stackoverflow
Solution 2 - Ruby on-RailsStefan Ciprian HotoleanuView Answer on Stackoverflow
Solution 3 - Ruby on-RailsmmikeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsablaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsKevin KohrtView Answer on Stackoverflow
Solution 6 - Ruby on-RailsArtur BilskiView Answer on Stackoverflow
Solution 7 - Ruby on-RailsSam SticklandView Answer on Stackoverflow
Solution 8 - Ruby on-RailsnachbarView Answer on Stackoverflow
Solution 9 - Ruby on-RailsSzymon RutView Answer on Stackoverflow
Solution 10 - Ruby on-RailsMikaeleView Answer on Stackoverflow
Solution 11 - Ruby on-RailszawhtutView Answer on Stackoverflow
Solution 12 - Ruby on-RailsBryan MemetiView Answer on Stackoverflow