Rails: is there an Engine.root?

Ruby on-Rails-3

Ruby on-Rails-3 Problem Overview


Rails.root returns a Path object specifying the root of a Rails project.

Is there an equivilent for Rails engines? Like Engine.root? If not, how else could I build up a path from the root of my Rails engine?

Ruby on-Rails-3 Solutions


Solution 1 - Ruby on-Rails-3

Lets say your engine file is set up like this:

module MyEngine
  class Engine < Rails::Engine
    #......
  end
end

You can call root on the Engine class like this:

MyEngine::Engine.root

Solution 2 - Ruby on-Rails-3

John's answer is right, but I'd clean that up a little bit like this:

When you mount your engine within your routes file add an alias first.

mount YourEngineNameHere::Engine => '/optional_namespace', as: 'your_engine_name'

Then do your_engine_name.root_url

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathan LongView Question on Stackoverflow
Solution 1 - Ruby on-Rails-3johnmcalileyView Answer on Stackoverflow
Solution 2 - Ruby on-Rails-3Nick ResView Answer on Stackoverflow