`method_missing': undefined method `devise' for User (call 'User.connection' to establish a connection)

前端 未结 9 1544
梦如初夏
梦如初夏 2020-12-20 11:29

I am new to Ruby on Rails and am trying to make my first application. I am having issues though anytime I run any rails generate command. I get the following er

相关标签:
9条回答
  • 2020-12-20 11:56

    Execute this following commands:

    rails generate devise:install
    rails generate devise MODEL
    

    If devise is installed already and also generated devise model as above and still facing error.

    Add this code to User.rb file in Models

    class User < ApplicationRecord
        extend Devise::Models
    
    0 讨论(0)
  • 2020-12-20 11:58

    rails g devise install != rails g devise:install

    I ran into this problem as well. Executing rails generate devise install (sic!) there was nothing that seemed not to have worked. The terminal output was:

    $ rails g devise install
      Running via Spring preloader in process 5422
          invoke  active_record
          create    db/migrate/20180306165306_devise_create_installs.rb
          create    app/models/install.rb
          insert    app/models/install.rb
           route  devise_for :installs
    

    But I missed the semicolon. Reverting these changes and then executing rails generate devise:install (note the : between devise and install), everything worked out perfectly. Also you get a much more extensive output:

    $ rails g devise:install
      Running via Spring preloader in process 5218
          create  config/initializers/devise.rb
          create  config/locales/devise.en.yml
      ===============================================================================
    
      Some setup you must do manually if you haven't yet:
    
        1. Ensure you have defined default url options in your environments files. Here
           is an example of default_url_options appropriate for a development environment
           in config/environments/development.rb:
    
             config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
    
           In production, :host should be set to the actual host of your application.
    
        2. Ensure you have defined root_url to *something* in your config/routes.rb.
           For example:
    
             root to: "home#index"
    
        3. Ensure you have flash messages in app/views/layouts/application.html.erb.
           For example:
    
             <p class="notice"><%= notice %></p>
             <p class="alert"><%= alert %></p>
    
        4. You can copy Devise views (for customization) to your app by running:
    
             rails g devise:views
    
      ===============================================================================
    
    0 讨论(0)
  • 2020-12-20 12:00

    I ran into a similar problem with an existing project freshly checked out from a repo:

    The specific missing files were:

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

    In this case, because the devise.rb was in the .gitignore list there was a config/initializers/devise.rb.example file that could be copied over.

    0 讨论(0)
  • 2020-12-20 12:09

    Or, like me, after adding:

    gem 'devise'
    

    to Gemfile, and running:

    $ bundle install
    

    you forgot to restart the server with Ctrl C and:

    $ rails s
    
    0 讨论(0)
  • 2020-12-20 12:11

    This is the appropriate Order.

    gem 'devise'
    Run the bundle command to install it.
    rails generate devise:install
    rails generate devise MODEL
    

    Here Model was created before the "rails generate devise:install".So you have to go back an comment everything that model created :

    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
    

    and also this

    Rails.application.routes.draw do
    #devise_for :users
    end
    

    Now,Run "rails generate devise:install" and after that uncomment the above .

    0 讨论(0)
  • 2020-12-20 12:13

    Running rails generate devise:install creates a config/initializers/devise.rb file.

    You have to restart your server with Ctrl + C and rails s in order for a change in config file to take effect.

    Otherwise you'll get the same error upon refreshing the page.

    0 讨论(0)
提交回复
热议问题