Ruby-on-rails 如何判断 rails 是否在生产中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1967809/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:05:15  来源:igfitidea点击:

How to tell if rails is in production?

ruby-on-railsdevelopment-environmentproduction-environment

提问by sent-hil

I used script/server -e productionto start rails in production mode. It did and I got no errors. However how do I tell if it is in production mode? I tried a non-existent route, and I got a similar error page I did in development.

我曾经script/server -e production在生产模式下启动 Rails。它做到了,我没有错误。但是,我如何判断它是否处于生产模式?我尝试了一条不存在的路线,我在开发中遇到了类似的错误页面。

I thought if under production model, I get the 404 error page that is in my /public folder.

我想如果在生产模式下,我会在我的 /public 文件夹中得到 404 错误页面。

Does it mean it didn't start in production mode?

这是否意味着它没有以生产模式启动?

Thanks for your help.

谢谢你的帮助。

采纳答案by Dan McNevin

2 easy ways:

2个简单的方法:

tail -f log/production.log

if there are entries populating that log after you hit the app, you're in production mode.

如果在您点击应用程序后有条目填充该日志,则您处于生产模式。

second way:

第二种方式:

in one of your views (probably the layout is good), just add

在您的其中一个视图中(可能布局很好),只需添加

<%= "Environment: #{RAILS_ENV}" %>

And that will show you what the environment that you're running in.

这将向您展示您正在运行的环境。

edit

编辑

You will see the default exception page instead of the actual error pages on any environment if the request is considered "local" (that is from localhost or 127.0.0.1), you can override this by adding this to your ApplicationController

如果请求被视为“本地”(即来自 localhost 或 127.0.0.1),您将在任何环境中看到默认异常页面而不是实际错误页面,您可以通过将其添加到您的 ApplicationController

def local_request?
  false
end

You can find this method in the docs in the api

您可以在api的文档中找到此方法

回答by Krishnaprasad Varma

If its Rails 3.1+, Rails.env.production?will return truewhen in production.

如果它的 Rails 3.1+,Rails.env.production?true在生产时返回。

Rails.env.production?  #=> true  
Rails.env.staging?     #=> false
Rails.env.development? #=> false  

回答by Dogweather

For modern Rails versions (3+), Rails.envreturns the environment as a String:

对于现代 Rails 版本(3+),Rails.env将环境返回为String

Rails.env #=> "production"

There are also helpful accessors for each environment that will return a Boolean:

每个环境还有一些有用的访问器,它们将返回一个Boolean

Rails.env.production?  #=> true  
Rails.env.staging?     #=> false
Rails.env.development? #=> false  

回答by Karolis Ramanauskas

On your command line type rails console, then Rails.env.

在您的命令行中键入rails console,然后Rails.env.

回答by bkunzi01

I found it much easier to just restart the rails server and read the second line on the command line:

我发现重新启动 rails 服务器并阅读命令行上的第二行要容易得多:

Running rails s -e productionoutputs:

运行rails s -e production输出:

=> Booting Puma
=> Rails 4.2.4 application starting in `production` on http://localhost:3000

Had Webrick in the example but some people didn't understand how changing servers would just substitute the name. Updated for clarity.

示例中有 Webrick,但有些人不明白更改服务器将如何替换名称。为清晰起见已更新。

回答by vireshas

command line alternative

命令行替代

$echo $RAILS_ENV

$echo $RAILS_ENV