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?
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
.
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 !
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With