xDOLLx96
@xDOLLx96

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

`const_get': uninitialized constant User (NameError)

解決したいこと

ここに解決したい内容を記載してください。

例)
Ruby on Railsでrails g model User name:string introduction:string profile_image_id:integer
上記を行ったところ以下のエラーが発生しました。
Running via Spring preloader in process 12155
invoke active_record
The name 'User' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.

Userが重複しているのかと思い rails destroy model user
で再度 rails g model User name:string introduction:string profile_image_id:integerを
行ったら以下のエラーが発生してしまいました。

発生している問題・エラー

vocstartsoft:~/environment/bookers2 (master) $ rails g model User name:string introduction:string profile_image_id:integer
/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/inflector/methods.rb:283:in `const_get': uninitialized constant User (NameError)
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/inflector/methods.rb:283:in `block in constantize'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/inflector/methods.rb:281:in `each'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/inflector/methods.rb:281:in `inject'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/inflector/methods.rb:281:in `constantize'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise.rb:316:in `get'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise/mapping.rb:83:in `to'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise/mapping.rb:78:in `modules'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise/mapping.rb:95:in `routes'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise/mapping.rb:162:in `default_used_route'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise/mapping.rb:72:in `initialize'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise.rb:346:in `new'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise.rb:346:in `add_mapping'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise/rails/routes.rb:243:in `block in devise_for'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise/rails/routes.rb:242:in `each'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/devise-4.8.0/lib/devise/rails/routes.rb:242:in `devise_for'
        from /home/ec2-user/environment/bookers2/config/routes.rb:2:in `block in <main>'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/routing/route_set.rb:432:in `instance_exec'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/routing/route_set.rb:432:in `eval_block'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/routing/route_set.rb:414:in `draw'
        from /home/ec2-user/environment/bookers2/config/routes.rb:1:in `<main>'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.7.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.6/lib/rails/application/routes_reloader.rb:41:in `block in load_paths'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.6/lib/rails/application/routes_reloader.rb:41:in `each'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.6/lib/rails/application/routes_reloader.rb:41:in `load_paths'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.6/lib/rails/application/routes_reloader.rb:20:in `reload!'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.6/lib/rails/application/routes_reloader.rb:30:in `block in updater'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/file_update_checker.rb:83:in `execute'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.6/lib/rails/application/routes_reloader.rb:10:in `execute'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.2.6/lib/rails/application/finisher.rb:143:in `block (2 levels) in <module:Finisher>'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:426:in `instance_exec'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:426:in `block in make_lambda'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:198:in `block (2 levels) in halting'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:606:in `block (2 levels) in default_terminator'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:605:in `catch'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:605:in `block in default_terminator'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:199:in `block in halting'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:513:in `block in invoke_before'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:513:in `each'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:513:in `invoke_before'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/callbacks.rb:131:in `run_callbacks'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/execution_wrapper.rb:110:in `run!'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/reloader.rb:115:in `run!'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/reloader.rb:53:in `block (2 levels) in reload!'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/reloader.rb:51:in `tap'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/reloader.rb:51:in `block in reload!'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/execution_wrapper.rb:87:in `wrap'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.2.6/lib/active_support/reloader.rb:50:in `reload!'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/application.rb:168:in `serve'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
        from /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from -e:1:in `<main>'

例)

NameError (uninitialized constant World)

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

該当するソースコード

ソースコードを入力

例)

def greet
  puts Hello World
end

自分で試したこと

ここに問題・エラーに対して試したことを記載してください。

0

1Answer

その前に rails g devise user を実行したことがあるならすでに User モデルが作られています。それを rails destroy model user で消しても devise の設定だけ残るためエラーになります。 config/routes.rb に devise_for :users があれば消してください。

また、ログインの仕組みを持たせた User モデルを作るなら rails g model User ... では不足です。 rails g devise user でモデルを作り、他のカラムが必要ならマイグレーションで追加してください。

0Like

Your answer might help someone💌