Rails:在初始化程序中获取主机名 [英] Rails: Get hostname in an initializer

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

问题描述

我正在使用Sorcery进行身份验证,并且需要在其初始化程序中设置第三方身份验证.

I'm using Sorcery for Authentication, and I need to setup third party authentication in its initializer.

初始化程序的一行看起来像这样:

The initializer has a line that looks like this:

config.twitter.callback_url "http://example.dev/auth/callback?provider=twitter"

...其中,当我在本地开发中使用Pow时,其中example.dev是主机名.如果应用程序正在生产中,则必须为example.com;如果应用程序正在生产中,则必须为staging.example.com.

...where example.dev is the hostname when I'm using Pow in local development. This needs to be example.com if the app is in production, or staging.example.com if it's in staging, etc.

我想将此行设置为类似这样的内容:

I would like to set this line to be something like this:

config.twitter.callback_url "#{Rails.hostname}/auth/callback?provider=twitter"

...但是request.host是我所知道的唯一方法,它知道这一点,并且仅在控制器级别可用.

... but request.host is the only method I know of that knows that and it's only available at the controller level.

我可以使用条件测试并为每个环境手动设置主机名,但是当我在不同的本地和暂存环境中进行测试时,能够以编程方式设置此设置将是很棒的选择.

I can use a conditional test and manually setup a hostname for each environment, but as I test on different local and staging environments it would be great to just be able to set this programatically.

有什么建议吗?

推荐答案

使用:

`hostname`

它使用Unix的主机名"实用程序,并返回一个字符串.

That uses the Unix "hostname" utility, and it returns a string.

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

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