31

I'm learning rails 4.1.5

I got this error:

2.1.1 :008 > Article
NameError: uninitialized constant Article::ImageUploader
    from /Volumes/disk0s4/www/rails/blog/app/models/article.rb:4:in `<class:Article>'
    from /Volumes/disk0s4/www/rails/blog/app/models/article.rb:1:in `<top (required)>'
    from (irb):8
    from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
    from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
    from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/didin/.rvm/gems/ruby-2.1.1/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
    from /Volumes/disk0s4/www/rails/blog/bin/rails:8:in `<top (required)>'
    from /Users/didin/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/didin/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

my file: article.rb

class Article < ActiveRecord::Base    
    validates_presence_of :title, :body
    belongs_to :user
    mount_uploader :image, ImageUploader
end

I got error when running rails console

when I write a word 'Article' on the console, it raises error above, but it working fine when this line mount_uploader :image, ImageUploader at article.rb's file removed.

when that line is restored, the error comes again. so it seems the error is caused that line, but I'm not sure.

anyone can fix this, please...

thank you for reading and answer :-)

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Didin Ahmadi
  • 491
  • 1
  • 7
  • 14

14 Answers14

71

I know this is a few months late but I stumbled across this issue myself. My solution was to paste

require 'carrierwave/orm/activerecord'

into the config/environment.rb file. Just append it at the end.

My Env: Ruby 2.1.2p95 ; Rails 4.1.7 ; Carrierwave-0.10.0

Max Rogers
  • 914
  • 7
  • 7
31

I added this to application.rb

require 'carrierwave'
require 'carrierwave/orm/activerecord'

Rails 4.2.0, Ruby 2.2.2, Carrierwave 0.10.0

PGill
  • 3,373
  • 18
  • 21
  • I'd like to point out in the CarrierWave docs it says: "Make sure you are loading CarrierWave after loading your ORM, otherwise you'll need to require the relevant extension manually" –  Apr 28 '16 at 22:29
  • First line is most likely not needed. It gets executed as part of `Bundler.require(*Rails.groups)` line. – x-yuri Apr 19 '17 at 15:31
27

Ill put it here, just in case..

IF you are using spring gem then you have to "restart" it by changing config/application.rb or close and open terminal, or: $ bin/spring stop

You can inspect its processes lifetime here(scroll to right):

$ ps aux | grep spring
alexey           55936   0.0  0.9  2645908  78440   ??  Ss   Thu06PM   0:13.17 spring app    | myapp | started 26 hours ago | development mode
alexey           81963   0.0  0.0  2481764   1608   ??  S    Sat11PM   0:01.91 spring server | myapp | started 141 hours ago

And kill it if needed.

More info at: https://github.com/rails/spring

alexey_the_cat
  • 1,812
  • 19
  • 33
10

Are you using spring?

I got two terminal windows, one for server, the other for console.

After I restarted my server and enter console again, the error disappeared.

And I didn't add any line in application.rb

Rails 4.2.4, Ruby 2.3.0, Carrierwave 0.10.0 a2c93fe

hungmi
  • 335
  • 7
  • 11
4

I'm assuming you're using CarrierWave gem for file uploads. Have you checked that it was properly installed? You could issue the command to check:

bundle show carrierwave

In my case, I've bundle installed on a different terminal where I ran rails console. HTH!

Maki
  • 890
  • 1
  • 7
  • 13
3

Another tip try: Open up the uploaded that was just generated. Ensure the name of the uploader class matches the name of the class you added in your model.

Adam Kalnas
  • 1,188
  • 2
  • 11
  • 25
2

If error appears in rails app' specs only you may be missing

require 'rails_helper'

at the top :)

Artur79
  • 12,705
  • 1
  • 22
  • 22
1

Exit and start your console again. In this case just reload! won't solve.

Seralto
  • 1,016
  • 1
  • 16
  • 30
1

I was getting this error and then it worked after restarting the server.

This occurs rather frequently. In development, the error crops up. The uploader was probably just added. Removing spring resolves, but that is a band-aid. The simple server re-start is often the proper solution.

Jerome
  • 5,583
  • 3
  • 33
  • 76
GraySmith00
  • 179
  • 1
  • 8
1

In my case I forgot to run rails g uploader image. Once done, it works fine.

John Doe
  • 596
  • 5
  • 8
0

try this in appliccation.rb correct module name this fix same problem for me

module CorrectName #OldName  <---------------
     # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true
  end
end
Bacho sh
  • 56
  • 6
0

For me, I uninstalled both the below gems

gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
0

In my case in my 'user.rb' module file, I had added the following lines:

  mount_uploader :photo, PhotoUploader
  mount_uploader :coverimage, CoverimageUploader

I had to quit them and

Feber Castellon
  • 509
  • 1
  • 7
  • 21
0

For anyone who has a preloader, such as Spring in place, before uninstalling the otherwise useful preloaded that speeds up your app, it's worth stopping it so that load paths will be reinitialised and any uploader in app/uploaders will be loaded once a new console or a rails process start. Try bin/spring stop. It will restart once it's necessary, now with the uploaders in place.

Attila Györffy
  • 759
  • 7
  • 13