Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Root directory path?

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

like image 964
fivetwentysix Avatar asked Sep 16 '10 07:09

fivetwentysix


People also ask

How do I find the root path?

In order to get the root directory path, you can use _DIR_ or dirname(). echo dirname(__FILE__); Both the above syntaxes will return the same result.

How do I get the current directory in Ruby?

pwd : To check the current working directory, pwd(present working directory) method is used. 5. chdir : To change the current working directory, chdir method is used.


2 Answers

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.

like image 200
Mischa Avatar answered Oct 18 '22 19:10

Mischa


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.

like image 44
malclocke Avatar answered Oct 18 '22 21:10

malclocke