4

I am deploying my first rails app using passenger and capistrano.

every thing is fine until i faced this error when I tried to launch the app in the browser.

Error message:
   uninitialized constant CarrierWave

Exception class:
   NameError

you can see the message in details at http://test.ajhezaty.com/

the site works perfectly locally and CarrierWave uploading the image correctly.

I tried to restart the VPS server by running

sudo /etc/init.d/httpd2 restart

but it didnt fix the issue.

for your information the gem installed on the server

 $ gem list | grep carrierwave
 carrierwave (0.6.1)
Abdulaziz Alsubaie
  • 710
  • 2
  • 8
  • 17

2 Answers2

15

You need to add carrierwave to your application.rb file like this:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

require 'carrierwave'

if defined?(Bundler)
  # Bundler stuff
end

# Rest of file ommited.

This worked for me in production using nginx/unicorn. Don't forget to restart your unicorn server.

Stephan1990
  • 445
  • 4
  • 19
  • Stefano, can you explain why this is required? It did work for me, but I haven't had to do it for any other gems. In fact, I didn't have to do it for CarrierWave, only for CarrierWave_Backgrounder. – Tyler Collier Jun 28 '12 at 01:42
  • @TylerCollier Unfortunately I have no idea. I just needed a solution quickly. – Stephan1990 Jul 05 '12 at 09:57
  • Best bet would be to ask on Github. I only use Carrierwave and don't know anything about it. – Stephan1990 Mar 25 '13 at 12:17
2

You may have the gem installed locally, but make sure you add it to your Gemfile, bundle install through ssh, and require it in the controllers that will use it.

DerektheDev
  • 93
  • 1
  • 9
  • gem list | grep carrierwave carrierwave (0.6.1) what do you mean by require it in the controllers that will use it? the application run locally without doing this! – Abdulaziz Alsubaie Apr 12 '12 at 03:36
  • 2
    In any controller that uses a gem, you should specify that requirement. So, for example, if you were using CarrierWave, go into your "pictures" controller, or whatever you called it, and type require 'carrierwave'. This ensures that it's being called. Your application works locally because gems are installed at the OS level... that is, it's not just a gem for Rails, it's a gem for all of Ruby. Therefore, in your Gemfile, you must specify what gems an application requires, so that upon deployment, the application gathers all of the dependencies at the OS level (the server). – DerektheDev Apr 12 '12 at 13:54
  • Just because you typed "gem install carrierwave" doesn't mean it's installed at the remote server. Hence the Gemfile, and bundle install on the remote server. – DerektheDev Apr 12 '12 at 13:58
  • I did what you said but still no luck – Abdulaziz Alsubaie Apr 12 '12 at 15:48
  • Same error? Check your production log ( rails root > log > production.log )... This is where the errors are kept--same stuff you see in the browser during development. Let me know what the log says. – DerektheDev Apr 12 '12 at 17:56
  • the log shows that every thing is migrated and completed successfully. I noticed something strange, when I cat the Gemfile from the deployment location it shows different content than the one I am working on in the deployment! how can I make them matched is there something to tell Cap command? – Abdulaziz Alsubaie Apr 12 '12 at 20:44
  • I've not used capistrano, but FTP/git deploy your Gemfile up there. That could be the issue. Your gemfile must be identical in both locations, or your app won't understand its dependencies. – DerektheDev Apr 12 '12 at 20:49