如何获取Rails应用程序的根目录路径?
当前回答
module Rails
def self.root
File.expand_path("..", __dir__)
end
end
来源: https://github.com/rails/rails/blob/5259062868dcf10fbcf735d6520e6a14e15fdcdb/actionmailer/test/abstract_unit.rb#L12
其他回答
只需编写Rails.root 并附加任何 Rails.root。Join (*%w(应用程序资产)).to_s
module Rails
def self.root
File.expand_path("..", __dir__)
end
end
来源: https://github.com/rails/rails/blob/5259062868dcf10fbcf735d6520e6a14e15fdcdb/actionmailer/test/abstract_unit.rb#L12
你可以使用:
Rails.root
但要加入您可以使用的资产:
Rails.root.join(*%w( app assets))
希望这能帮到你。
为了获得更高的正确性,你应该使用:
Rails.root.join('foo','bar')
这将允许您的应用程序在/不是目录分隔符的平台上工作,如果有人尝试在其中运行它。
您可以通过变量RAILS_ROOT访问rails应用程序路径。
例如:
render :file => "#{RAILS_ROOT}/public/layouts/mylayout.html.erb"
推荐文章
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- ActiveRecord OR查询
- 无法安装gem -未能建立gem本地扩展-无法加载这样的文件——mkmf (LoadError)
- 在Rails中使用user_id:integer vs user:references生成模型
- 如何测试参数是否存在在轨道
- 验证多个列的唯一性
- Rails:在where语句中使用大于/小于
- Rails:如何为Rails activerecord的模型中的属性创建默认值?
- 库未加载:libmysqlclient.16。在OS X 10.6上使用mysql2 gem运行'rails server'时出现dylib错误
- 如何确定一个数组是否包含另一个数组的所有元素
- Rails find_or_create_by多个属性?
- Rails ActiveRecord日期之间
- 是什么导致这个ActiveRecord::ReadOnlyRecord错误?
- Rails:dependent =>:destroy VS:dependent =>:delete_all
- 对于PostgreSQL表来说,多大才算太大?