Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails precompile constant uninitialized

I wanted to preload the configuration (from ".yml" files). In one of my initializer files (config/initializers/facebook.rb) I have following line of code:

FACEBOOK_CONFIG = YAML.load_file("#{Rails.root}/config/facebook.yml")[Rails.env]

So, it works like a charm in the "DEVELOPMENT" mode. Once I switch to the production mode, it keeps telling me, that FACEBOOK_CONFIG is an uninitialized constant for my "facebook.js.coffee.erb" file, located in assets/javascript (If it matters), if I want to o "rake assets:precompile". I've tried doing random stuff, like: RAILS_ENV=production bundle exec rake assets:precompile or

rake assets:precompile:all

, but no luck

I have tried assigning "initialize_on_precompile = true" variable for my production environment (although, it should be true by default), just in case.

Why it doesn't work in production mode (But, I want to emphasise, that it does work(!) in the development environment).

Can someone help with that one ?

like image 813
Dmitri Avatar asked Oct 04 '22 08:10

Dmitri


1 Answers

I encountered exactly the same problem. This is because your javascript(coffescript) file makes reference to a constant that is defined in an initializer. Because it is precompiled before the initializer the app throws an error.

This is the simple solution I found. You place this code at the bottom of your application.rb file in config:

module AssetsInitializers
  class Railtie < Rails::Railtie
    initializer "assets_initializers.initialize_rails",
                :group => :assets do |app|
      require "#{Rails.root}/config/initializers/facebook.rb" 
    end
  end
end

It manually loads up certain files from the initializer folder. It solved my problem.

Hopefully this was the issue for you as well.

like image 72
Laurent Avatar answered Oct 10 '22 21:10

Laurent