Search code examples
ruby-on-railsrubydevise

undefined method 'devise' for User


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?


Solution

  • 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.