Rails Root directory path?

Ruby on-Rails

Ruby on-Rails Problem Overview


How do I get my Rails app's root directory path?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

In Rails 3 and newer:

Rails.root

which returns a Pathname object. If you want a string you have to add .to_s. If you want another path in your Rails app, you can use join like this:

Rails.root.join('app', 'assets', 'images', 'logo.png')

In Rails 2 you can use the RAILS_ROOT constant, which is a string.

Solution 2 - Ruby on-Rails

For super correctness, you should use:

Rails.root.join('foo','bar')

which will allow your app to work on platforms where / is not the directory separator, should anyone try and run it on one.

Solution 3 - Ruby on-Rails

You can access rails app path using variable RAILS_ROOT.

For example:

render :file => "#{RAILS_ROOT}/public/layouts/mylayout.html.erb"

Solution 4 - Ruby on-Rails

In addition to all the other correct answers, since Rails.root is a Pathname object, this won't work:

Rails.root + '/app/assets/...'

You could use something like join

Rails.root.join('app', 'assets')

If you want a string use this:

Rails.root.join('app', 'assets').to_s

Solution 5 - Ruby on-Rails

In some cases you may want the Rails root without having to load Rails.

For example, you get a quicker feedback cycle when TDD'ing models that do not depend on Rails by requiring spec_helper instead of rails_helper.

# spec/spec_helper.rb

require 'pathname'

rails_root = Pathname.new('..').expand_path(File.dirname(__FILE__))

[
  rails_root.join('app', 'models'),
  # Add your decorators, services, etc.
].each do |path|
  $LOAD_PATH.unshift path.to_s
end

Which allows you to easily load Plain Old Ruby Objects from their spec files.

# spec/models/poro_spec.rb

require 'spec_helper'

require 'poro'

RSpec.describe ...

Solution 6 - Ruby on-Rails

Solution 7 - Ruby on-Rails

You can use:

Rails.root

But to to join the assets you can use:

Rails.root.join(*%w( app assets))

Hopefully this helps you.

Solution 8 - Ruby on-Rails

Simply by Rails.root or if you want append something we can use it like Rails.root.join('app', 'assets').to_s

Solution 9 - Ruby on-Rails

Simply By writing Rails.root and append anything by Rails.root.join(*%w( app assets)).to_s

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
QuestionfivetwentysixView Question on Stackoverflow
Solution 1 - Ruby on-RailsMischaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsmalclockeView Answer on Stackoverflow
Solution 3 - Ruby on-RailsAnubhawView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAndrewView Answer on Stackoverflow
Solution 5 - Ruby on-RailsDennisView Answer on Stackoverflow
Solution 6 - Ruby on-Railsvon spotzView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAli Hassan MirzaView Answer on Stackoverflow
Solution 8 - Ruby on-RailsAmitView Answer on Stackoverflow
Solution 9 - Ruby on-RailssumitView Answer on Stackoverflow