Problem creating devise User + Ruby on Rails

0

I'm trying to put an entity that already had in my project how to devise to login and etc, I used the following commands

add in Gemfile the line gem 'devise' on the terminal I run:

rails generate devise:install
rails generate devise User

After doing this he generated a file to change in the database:

#20160114174028_add_devise_to_users.rb
class AddDeviseToUsers < ActiveRecord::Migration
  def self.up
    change_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      # Uncomment below if timestamps were not included in your original model.
      # t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end

  def self.down
    # By default, we don't want to make any assumption about how to roll back a migration when your
    # model already existed. Please edit below which fields you would like to remove in this migration.
    raise ActiveRecord::IrreversibleMigration
  end
end

#routes.rb
Rails.application.routes.draw do
  namespace :api do
    devise_for :users

    resources :schedules, except: [:new, :edit]
    # resources :users, except: [:new, :edit]
  end
end

and my model it added a lines like this:

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

The problem is that when I was given the command devise_for :users gave the following output:

== 20160113192834 CreateSchedules: migrating ==================================
-- create_table(:schedules)
   -> 0.0022s
== 20160113192834 CreateSchedules: migrated (0.0027s) =========================

rake aborted!
NameError: uninitialized constant CreateUsers
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:261:in 'const_get'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:261:in 'block in constantize'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:259:in 'each'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:259:in 'inject'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:259:in 'constantize'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/core_ext/string/inflections.rb:66:in 'constantize'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:775:in 'load_migration'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:770:in 'migration'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in 'disable_ddl_transaction'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:1048:in 'use_transaction?'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:958:in 'rescue in block in migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:955:in 'block in migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in 'each'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in 'migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in 'up'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:798:in 'migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:in 'migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:44:in 'block (2 levels) in <top (required)>'
NameError: uninitialized constant CreateUsers
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:261:in 'const_get'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:261:in 'block in constantize'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:259:in 'each'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:259:in 'inject'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:259:in 'constantize'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activesupport-4.2.4/lib/active_support/core_ext/string/inflections.rb:66:in 'constantize'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:775:in 'load_migration'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:770:in 'migration'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in 'disable_ddl_transaction'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:1048:in 'use_transaction?'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:1040:in 'ddl_transaction'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:994:in 'execute_migration_in_transaction'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:956:in 'block in migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in 'each'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in 'migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in 'up'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/migration.rb:798:in 'migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:in 'migrate'
/home/vagrant/.rvm/gems/ruby-2.2.0@rails_4_2_jet2go/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:44:in 'block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

A similar error had already happened to me and I just corrected it by giving rake db:migrate , but this time it did not work.

This has happened to someone else at the time of generating the devise, and someone better knows how to make it work and why it happens?

    
asked by anonymous 14.01.2016 / 18:57

1 answer

0

Then I found out. This happens when the name of the migration class is different from the file name.

For example, my creation file was: 20160114164439_create_users.rb and my class was class AddDeviseToUsers < ActiveRecord::Migration , just correct the class name for class CreateUsers < ActiveRecord::Migration .

Then just give the command: rake db:migrate

    
14.01.2016 / 19:31