Didin Ahmadi
Didin Ahmadi

Reputation: 491

Rails: NameError: uninitialized constant Article::ImageUploader when using Carrierwave on rails 4.1.5

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

Upvotes: 31

Views: 27589

Answers (14)

Attila Gy&#246;rffy
Attila Gy&#246;rffy

Reputation: 759

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.

Upvotes: 0

GraySmith00
GraySmith00

Reputation: 309

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.

Upvotes: 1

Feber Castellon
Feber Castellon

Reputation: 579

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

Upvotes: 0

Aniket Tiwari
Aniket Tiwari

Reputation: 3998

For me, I uninstalled both the below gems

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

Upvotes: 0

John Doe
John Doe

Reputation: 676

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

Upvotes: 1

Artur79
Artur79

Reputation: 13637

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

require 'rails_helper'

at the top :)

Upvotes: 2

Bacho sh
Bacho sh

Reputation: 56

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

Upvotes: 0

alexey_the_cat
alexey_the_cat

Reputation: 1872

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

Upvotes: 27

hungmi
hungmi

Reputation: 395

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

Upvotes: 10

Seralto
Seralto

Reputation: 1076

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

Upvotes: 1

PGill
PGill

Reputation: 3521

I added this to application.rb

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

Rails 4.2.0, Ruby 2.2.2, Carrierwave 0.10.0

Upvotes: 31

Adam Kalnas
Adam Kalnas

Reputation: 1198

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.

Upvotes: 3

Max Rogers
Max Rogers

Reputation: 914

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

Upvotes: 71

Maki
Maki

Reputation: 980

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!

Upvotes: 4

Related Questions