From a clean create-repack-app install. I add the following to my Gemfile then run bundle:
gem 'devise_token_auth'
Then I run:
rake db:create
rails g devise_token_auth:install
rake db:migrate
Databases (dev and test) are created and ruby files generated (including an addition to the config/routes.rb file). Trying any rake or rails command does the following right now:
rake routes
rake aborted!
NoMethodError: undefined method `devise' for User (call 'User.connection' to establish a connection):Class
Commenting out the following in the config/routes.rb file:
mount_devise_token_auth_for 'User', at: 'auth'
Removes this error. The code added to the User model doesn't contribute to this error. Do I need to run rails g devise:install also? The documentation doesn't mention anything extra. So I'm not sure what I'm doing wrong.
Add below code to the User model
extend Devise::Models
My User model looks like this.
# frozen_string_literal: true
class User < ActiveRecord::Base
extend Devise::Models
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable, :trackable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
include DeviseTokenAuth::Concerns::User
end
I found my answer from another post: Devise_token_auth conflicts?
Adding the following Devise initializer:
config/devise.rb:
Devise.setup do |config|
# The e-mail address that mail will appear to be sent from
# If absent, mail is sent from "please-change-me-at-config-initializers-devise#example.com"
config.mailer_sender = "support#myapp.com"
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default) and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
# available as additional gems.
require 'devise/orm/active_record'
# If using rails-api, you may want to tell devise to not use ActionDispatch::Flash
# middleware b/c rails-api does not include it.
# See: https://stackoverflow.com/q/19600905/806956
config.navigational_formats = [:json]
end
Fixed the problem.
I am trying to set up Pageflow. Finally I managed to create DBs and install pageflow. I can run rails s but I get Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue. in my browser.
Anyway, typing bundle exec rake db:create db:migrate would give me this error:
== AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
undefined method `to_sym' for {:null=>false, :default=>""}:Hash/Users/user/.gem/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:671:in `type_to_sql'
This line in schema_statements says.
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
if native = native_database_types[type.to_sym]
column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup
if type == :decimal # ignore limit, use precision and scale
scale ||= native[:scale]
if precision ||= native[:precision]
if scale
column_type_sql << "(#{precision},#{scale})"
else
column_type_sql << "(#{precision})"
end
elsif scale
raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale is specified"
end
elsif (type != :primary_key) && (limit ||= native.is_a?(Hash) && native[:limit])
column_type_sql << "(#{limit})"
end
column_type_sql
else
type
end
end
I tried to solve this with the gem composite-primary-keys, but found out that there is no compatible version for rails 4.0.2.
Being an absolute noob, I found the issue here: https://github.com/composite-primary-keys/composite_primary_keys/issues/174 but it didn't helped me out.
Thanks!
//EDIT
Thats my user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
include Pageflow::UserMixin
end
There is no extra comma at the end...
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.
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.
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)".
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
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.
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'
I had to stop the Spring preloader process:
spring stop
Now works as expected
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
I run into this issue as well, solved it by :
Steps:
Comment out all references to devise most importantly in the routes.rb and
user.rb model
run rails g devise:install and be sure you follow the suggested steps
in the guide on screen.
Go on and uncomment references to devise in routes.rb and user.rb
run rake db:migrate
And you good to go!
comment your app/model/user.rb and also comment devise route in config/routes.rb
`rails generate devise:install`
then run
`rails db:migrate`
then uncomment all done before
this method work for me on rails 7.0.0.4
Also ran into the same problem.
For your case, you should first of all stop the server, and restart it. This should work out!
For more insight, you should also consier visiting this site; https://edgeguides.rubyonrails.org/routing.html
I got the same issue now with a lagacy system. The problem was caused by a commented line (remained there at migration from Rails 4 to 5) in the header of config/routes.rb:
#V2p0::Application.routes.draw do
devise_for :users
Rails.application.routes.draw do
devise_for :users
The devise initialization script inserted devise_for also below the commented line. After removing it the problem disappeared.
Comment out:-
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
in user.rb Model. This worked for me
I'm using devise in my Admin model like so:
class Admin < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Functionally, this is working fine. But I want to have specs for my Admin class, so I start to write a spec...
require 'spec_helper'
require 'admin'
describe Admin do
it 'mumblemumble' do ... end
end
...and then when I go to run the spec, it blows up:
active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `devise' for Admin(Table doesn't exist):Class (NoMethodError)
How do I require Devise so that it's available in my spec? It seems like I should be able to
require 'devise'
in either my spec or my model (preferably my model), but this doesn't fix the issue or change my stack trace. How do I require Devise so that my model has the helper method available? I'm poking around the Devise gem but I'm not finding any clues.
How are you running these? RSpec directly? Or bundle exec rake spec?
There's this in your error: for Admin(Table doesn't exist) which makes me wonder if you have a database yet. The rake task should take care of setting up your world for you.
If that doesn't help, post your spec_helper.rb contents too.
Here's a basic Admin model I have:
class Admin < ActiveRecord::Base
devise :database_authenticatable, :recoverable, :rememberable,
:trackable, :validatable
end
And a basic spec:
require 'spec_helper'
describe Admin do
it { should validate_uniqueness_of(:email) }
end
Works great with vanilla generated rails app and generated devise setup.
This error
undefined method `devise' for Admin(Table doesn't exist):Class (NoMethodError)
seems to be that you don't have the table in the db? Did you migrate the rake file?
Yes it seems your test database does not have the admins table.
Try this:
bundle exec rake db:migrate db:test:prepare
db:migrate migrates your development database, if there are any pending migrations and db:test:prepare clones your test database according to the development one.
Got it. I run specs with an in-memory instance of sqlite3, so all the db:test:prepare doesn't apply to me. In addition to requiring Devise, it must also be setup/configured.
So in /spec/support/devise.rb:
require 'devise'
Devise.setup do |config|
require 'devise/orm/active_record'
end
And then in spec_helper.rb:
Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
I have an rails 3 (3.0.9) application working on ruby 1.8.7 with the gem devise (1.4.2) on my computer working perfectly.
I tried to push it on heroku and i got the following error message on application load:
[WARNING] You provided devise_for :users but there is no model User defined in your application
=> Booting WEBrick
=> Rails 3.0.9 application starting in production on http://0.0.0.0:43292
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:124:in `block in constantize': uninitialized constant User (NameError)
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:123:in `each'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:123:in `constantize'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:84:in `to'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:79:in `modules'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:88:in `strategies'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:410:in `block in configure_warden!'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:409:in `each_value'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:409:in `configure_warden!'
The problem comes from devise but i don't know how to fix it.
My model User is correctly defined, and it works on my computer...
Does anyone know how to fix this ?
Thanks for you help
Make sure you have this:
class User < ActiveRecord::Base
Defined at the top of your user/devise model.
ALso, make sure you run your migrations.
heroku rake db:migrate
In console.
Check inside your users.rb to make sure that you spelled ':database_authenticable' correctly, this seems to be a common problem with this error.
class User < ActiveRecord::Base
devise :database_authenticable, :recoverable,
:rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
Also, I suggest you take a look here, to see how these people fixed the error.
http://groups.google.com/group/plataformatec-devise/browse_thread/thread/807f4c6e3475622f