Rails:在初始化程序中获取主机名

编程入门 行业动态 更新时间:2024-10-28 08:26:49
本文介绍了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 "example.dev/auth/callback?provider=twitter"

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

...where example.dev is the hostname when I'm using Pow in local development. This needs to be example if the app is in production, or staging.example 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:在初始化程序中获取主机名

本文发布于:2023-08-07 14:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   主机名   程序   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!