如何在Rails初始化程序中获取服务器端口 [英] How to get the server port in a Rails initializer

查看:101
本文介绍了如何在Rails初始化程序中获取服务器端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个Rails服务器实例,并且每个实例都需要在environment.rb中知道其自己的侦听端口. request.port将在控制器中工作,但不能在environment.rb的上下文中工作.有办法吗?谢谢!

I have multiple instances of Rails servers and there is a need for each one of them to know its own listening port in environment.rb. request.port will work in the controllers but not in the context of the environment.rb. Is there a way? Thanks!

推荐答案

您可以使用以下代码获取运行中的rails端口:

You can get the running rails port using this code:

Rails::Server.new.options[:Port]

您可以查看选项Rails文档中的解析器,以查看可用的哈希参数.

You can take a look at the options parser in the Rails documentation to see what hash parameters are available.

这篇关于如何在Rails初始化程序中获取服务器端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆