331

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

9 Answers 9

586

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.

4
  • 3
    In Rails 2.3 Rails.root is an instance of Pathname where RAILS_ROOT is a string.
    – Richard
    Jun 9, 2011 at 9:54
  • 1
    Only Rails.root in 3.1 and later (ahh.. the case of changing CONSTANT ;))
    – thanikkal
    Nov 10, 2011 at 13:35
  • 2
    You can also Rails.root.join(*%w( app assets images logo.png )).
    – Nate
    Aug 12, 2014 at 18:21
  • 18
    Personally I like the newer syntax: Rails.root / 'app' / 'assets' / 'images' / 'logo.png'
    – Ajedi32
    Feb 18, 2016 at 17:13
111

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.

1
  • 1
    For example, on my MacBook, Rails.root.join('foo','bar') evaluates to Pathname object whose @path is '/Users/purplejacket/my_rails_app/foo/bar' Sep 7, 2012 at 0:58
24

You can access rails app path using variable RAILS_ROOT.

For example:

render :file => "#{RAILS_ROOT}/public/layouts/mylayout.html.erb"
1
  • 4
    RAILS_ROOT is deprated since Rails 3.0 insted use Rails.root
    – vidur punj
    Nov 23, 2017 at 14:55
16

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
2
  • 1
    Actually Rails.root + 'app/assets' does work, but yeah join is neater.
    – Mischa
    Oct 23, 2012 at 13:57
  • 1
    It's usually not a good idea to hardcode what the file separator token is (\ or /). Mar 15, 2013 at 15:05
6

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 ...
2
module Rails
  def self.root
    File.expand_path("..", __dir__)
  end
end

source: https://github.com/rails/rails/blob/5259062868dcf10fbcf735d6520e6a14e15fdcdb/actionmailer/test/abstract_unit.rb#L12

0

You can use:

Rails.root

But to to join the assets you can use:

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

Hopefully this helps you.

-4

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

1
  • 1
    Please delete this answer, it is just noise, does not add anything to the question.
    – luk2302
    May 25, 2017 at 18:14
-8

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

1
  • 2
    Please delete this answer, it is just noise, does not add anything to the question and it is poorly formatted.
    – luk2302
    May 25, 2017 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.