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 :-)

14 Answers 14

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I skipped over this while following the guide.
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

2 Comments

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"
First line is most likely not needed. It gets executed as part of Bundler.require(*Rails.groups) line.
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

3 Comments

Adding spaces to config/application.rb and saving worked for me!
Worth noting, I tried restarting the server, and guard, but modifying the application.rb file did it for me (whitespace). I never tried restarting terminal, but now I'n guessing that also would have done it.
Just stopping spring gem using bin/spring stop did it for me. Thanks.
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

Comments

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!

Comments

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.

Comments

2

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

require 'rails_helper'

at the top :)

Comments

1

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

Comments

1

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

Comments

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.

Comments

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

Comments

0

For me, I uninstalled both the below gems

gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.