Ruby-on-rails Rails 根目录路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3724487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails Root directory path?
提问by fivetwentysix
How do I get my Rails app's root directory path?
如何获取 Rails 应用程序的根目录路径?
回答by Mischa
In Rails 3 and newer:
在 Rails 3 和更新版本中:
Rails.root
which returns a Pathnameobject. If you want a string you have to add .to_s. If you want another path in your Rails app, you can use joinlike this:
它返回一个Pathname对象。如果你想要一个字符串,你必须添加.to_s. 如果你想在你的 Rails 应用程序中使用另一个路径,你可以join像这样使用:
Rails.root.join('app', 'assets', 'images', 'logo.png')
In Rails 2 you can use the RAILS_ROOTconstant, which is a string.
在 Rails 2 中,您可以使用RAILS_ROOT常量,它是一个字符串。
回答by malclocke
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.
这将允许您的应用程序在/不是目录分隔符的平台上工作,如果有人尝试在一个平台上运行它。
回答by Anubhaw
You can access rails app path using variable RAILS_ROOT.
您可以使用变量访问 rails 应用程序路径RAILS_ROOT。
For example:
例如:
render :file => "#{RAILS_ROOT}/public/layouts/mylayout.html.erb"
回答by Andrew
In addition to all the other correct answers, since Rails.rootis a Pathnameobject, this won't work:
除了所有其他正确答案之外,由于Rails.root是一个Pathname对象,这将不起作用:
Rails.root + '/app/assets/...'
You could use something like join
你可以使用类似的东西 join
Rails.root.join('app', 'assets')
If you want a string use this:
如果你想要一个字符串使用这个:
Rails.root.join('app', 'assets').to_s
回答by Dennis
In some cases you may want the Rails root without having to load Rails.
在某些情况下,您可能需要 Rails 根目录而不必加载 Rails。
For example, you get a quicker feedback cycle when TDD'ing models that do not depend on Rails by requiring spec_helperinstead of rails_helper.
例如,当 TDD 模型不依赖于 Rails 时,通过要求spec_helper而不是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.
这使您可以轻松地从规范文件中加载普通的旧 Ruby 对象。
# spec/models/poro_spec.rb
require 'spec_helper'
require 'poro'
RSpec.describe ...
回答by Ali Hassan Mirza
You can use:
您可以使用:
Rails.root
But to to join the assets you can use:
但是要加入您可以使用的资产:
Rails.root.join(*%w( app assets))
Hopefully this helps you.
希望这对你有帮助。
回答by Amit
Simply by Rails.root or if you want append something we can use it like Rails.root.join('app', 'assets').to_s
简单地通过 Rails.root 或者如果你想要附加一些东西我们可以使用它像 Rails.root.join('app', 'assets').to_s
回答by sumit
Simply By writing Rails.rootand append anything by Rails.root.join(*%w( app assets)).to_s
只需编写Rails.root并通过Rails.root.join(*%w( app assets)).to_s附加任何内容

