Ruby-on-rails 如何访问 Rails 3 应用程序对象的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3539148/
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
How do I access the name of the Rails 3 application object?
提问by Steve
I need to know the name of a programmers application from within a Rails 3 engine.
我需要从 Rails 3 引擎中知道程序员应用程序的名称。
回答by Andrei Kulakov
Rails.application.class.parent_name
回答by Tilo
The original poster asked for the name of the rails app, not for the class name. Those are two different things. e.g. the spelling of the rails app can be different from what Rails expects, e.g. 'test-app' instead of 'test_app'.
原始海报要求提供rails app 的名称,而不是类名。这是两个不同的东西。例如,rails 应用程序的拼写可能与 Rails 期望的不同,例如“test-app”而不是“test_app”。
It is difficult to get to the name of the Rails App spelled the way as it was checked-in, because that name is not preserved. Only the options for the session_store seem to contain the original string slightly modified.
很难找到Rails 应用程序的名称,因为它的拼写方式是签入的,因为该名称不会被保留。只有 session_store 的选项似乎包含稍微修改过的原始字符串。
The best way to get to the name of the Rails application is:
获得 Rails 应用程序名称的最佳方法是:
This will work even if your app directory was renamed, or sym-linked!
即使您的应用程序目录被重命名或符号链接,这也将起作用!
Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
=> "test-app"
Why? Because the author of the app could have spelled it's namedifferently than Rails expects... e.g. with '-' characters instead of '_'; e.g. "test-app". From the class name you can't guarantee to get to the correct spelling.
为什么?因为应用程序的作者可能拼写它的名称与 Rails 预期的不同......例如使用“-”字符而不是“_”;例如“测试应用程序”。从类名你不能保证得到正确的拼写。
Using this info, you could do this:
使用此信息,您可以执行以下操作:
class << Rails.application
def name
Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
end
end
Rails.application.name
=> 'test-app'
or just add this to your ./config/environment.rb:
或者只是将其添加到您的./config/environment.rb:
APP_VERSION = '1.0.0'
APP_NAME = Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
that makes these constants available on the top-level of the app.
这使得这些常量在应用程序的顶层可用。
Close, but no cigar:
关闭,但没有雪茄:
This is almost correct, but it will still fail if the application directory gets renamed (e.g. during deployment to '"20121001_110512" or "latest"... then the following would break:
这几乎是正确的,但如果应用程序目录被重命名,它仍然会失败(例如,在部署到“20121001_110512”或“最新”期间……那么以下内容会中断:
File.basename(Rails.root.to_s)
=> "test-app"
with the following two approaches you can't get to the correct spelling.. you could only guess a name:
使用以下两种方法您无法获得正确的拼写..您只能猜测一个名称:
This is sub-optimal, and can give incorrectly spelled results:
这是次优的,可能会给出拼写错误的结果:
You can get to a derivative of the application name like this:
您可以像这样获得应用程序名称的派生词:
Rails.application.engine_name.gsub(/_application/,'')
=> "test_app"
But please note that this is not fool-proof, because somebody could have named the app "test-app" and you will see the result above, not the correct name with '-'.
但请注意,这并不是万无一失的,因为有人可以将应用命名为“test-app”,您将看到上面的结果,而不是带有“-”的正确名称。
Same thing is true if you derive it like this:
如果你这样推导它,同样的事情也是如此:
Rails.application.class.parent_name
=> "TestApp"
this is the class name, but you can't be sure how to get to the name the way the author spelled it.
这是班级名称,但您无法确定如何按照作者拼写的方式获得名称。
回答by Scott
In Rails 3, the application that is generated is given a module namespace matching the application name. So if your application was called "Twitter", the class of Rails.application is "Twitter::Application".
在 Rails 3 中,生成的应用程序被赋予一个与应用程序名称匹配的模块命名空间。因此,如果您的应用程序被称为“Twitter”,则 Rails.application 的类是“Twitter::Application”。
This means that you could split the string of the class name of your Rails application to get an application name like this:
这意味着您可以拆分 Rails 应用程序的类名字符串以获得如下所示的应用程序名称:
Rails.application.class.to_s.split("::").first
In our example, the resulting string would be "Twitter".
在我们的示例中,结果字符串将是“Twitter”。
回答by yfeldblum
Rails.applicationreturns your MyApp::Applicationclass.
Rails.application返回您的MyApp::Application课程。
回答by Roxas Shadow
For a more "humanized" version (based on the first reply you can find here) consider the following method: it will return Test Appfor your TestAppapplication.
对于更“人性化”的版本(基于您可以在此处找到的第一个回复),请考虑以下方法:它将Test App为您的TestApp应用程序返回。
def app_name
Rails.application.class.parent_name
.underscore
.humanize
.split
.map(&:capitalize)
.join(' ')
end
回答by Peter Lyons
Using Scott's answer, I put the following in my app/controllers/application_controller.rbfile
使用 Scott 的回答,我将以下内容放入我的app/controllers/application_controller.rb文件中
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :app_name
private
def app_name
Rails.application.class.to_s.split("::").first
end
Then in my templates I can just do #{app_name}wherever it's needed, which makes it much less of a hassle if you need to rename your app (which I had to do this morning).
然后在我的模板中,我可以在#{app_name}任何需要的地方做,如果你需要重命名你的应用程序(我今天早上必须这样做),这会减少很多麻烦。
回答by Tate Thurston
For Rails 6 and beyond
对于 Rails 6 及更高版本
Rails.application.class.module_parent.name
Rails.application.class.module_parent.name
More context:
更多背景:
Module#parenthas been renamed to module_parent. parentis deprecated and will be removed in Rails 6.1.
Module#parent已更名为module_parent. parent已弃用,将在 Rails 6.1 中删除。
回答by Andrew
You can find this in the config.ru file:
您可以在 config.ru 文件中找到它:
run Dinosaur::Application
or in the Rakefile:
或在 Rakefile 中:
Dinosaur::Application.load_tasks
The name prior to the "::" is the app name
“::”之前的名称是应用程序名称

