Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails.root from engine

I'm having some problem accessing Rails.root from my rails engine, that I'm creating. I need to fetch a yml config file from the main app.

Is there any "best practices" for handling configurations for your engines?

like image 383
sandelius Avatar asked Jul 23 '12 15:07

sandelius


People also ask

How do you get a Rails route?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

How does a Rails engine work?

Rails looks first in the application's ( test/dummy ) app/views directory and then in the engine's app/views directory. When it can't find it, it will throw this error. The engine knows to look for blorgh/comments/_comment because the model object it is receiving is from the Blorgh::Comment class.

What is Mount in Rails?

Mounting rails are constructive items in electrical engineering and mechanical engineering projects; they are used to hold devices. A mounting rail is usually attached to a mounting panel or an enclosure profile.


1 Answers

Let's assume you have a module attribute for that.

# lib/my_engine.rb

module MyEngine

  mattr_accessor :app_root

end

Then you can load it from the initialize block like so:

# lib/my_engine/engine.rb

module MyEngine

  class Engine < Rails::Engine

    initializer "my_engine.load_app_root" do |app|

       MyEngine.app_root = app.root

    end

  end

end
like image 139
shime Avatar answered Sep 23 '22 15:09

shime