简体   繁体   中英

Rails: You cannot have more than one Rails::Application (RuntimeError)

I installed a pesapal gem with an initializer file that looks like this;

# Load Pesapal config file when applicatin is loaded ... the config can then be
# accessed from PesapalRails::Application.config.yaml
module PesapalRails
  class Application < Rails::Application
    config.yaml = YAML::load(IO.read("#{Rails.root}/config/pesapal.yml"))
  end
end

When i use it i get the error like this;

/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application.rb:63:in ``inherited'``: You cannot have more than one Rails::Application (RuntimeError)

With a partial trace looking like this;

from /var/www/html/webapp/config/initializers/pesapal.rb:4:in `<module:PesapalRails>'
from /var/www/html/webapp/config/initializers/pesapal.rb:3:in `<top (required)>'

Fix

Upgrade the gem, v1.2.1 should fix the problem ( changelog )

Short Explanation

Simple version ... my approach was wrong - sincere apologies. New to Ruby. New to Rails.

Long Explanation

The initializer was meant to create a global variable that you could access in your rails app. In my case it was PesapalRails::Application.config.yaml . I'd assume in your case, it was different.

The gem assumed that that global variable was set, correctly, which would bring an error since in your app PesapalRails doesn't exist (as you would know, the initializer is ran only when the app is started so this was kinda a safe assumption). This went undetected in my case since my demo app was actually using the PesapalRails namespace.

Request

Wish you would have filed an issue on Github - here next time (it's sheer luck I stumbled on this) ... this way other devs might actually contribute to the discussion/solution. I'm willing to review and push up updates asap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM