ruby-on-rails - Rails : how to get the current host, 或用它构建 URL?

标签 ruby-on-rails ruby url host

<分区>

我需要使用主机构建具有不同端口的 URL。

例如,如果主机是 example.com,我需要生成类似 http://example.com:8080/ 的 URL

我需要它是便携的,所以当我在本地环境中时,它会显示 http://localhost:8080/相反。

有什么想法吗?

最佳答案

我经常在 ApplicationController 中使用 before_filter 来设置带有主机名的实例变量,如下所示:

@hostname = request.host || "www.mydomain.com"

您还可以使用 request.port 获取请求通过的端口号(取自 HTTP header )。

关于ruby-on-rails - Rails : how to get the current host, 或用它构建 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9452917/

相关文章:

php - 删除 URL 中的 UTF-8 DOM - JS 和 PHP

iphone - 为现有的 Rails 应用程序创建 iphone 和 android 应用程序

ruby-on-rails - rails : Grape API - NameError: uninitialized constant API

Ruby - 命令行窗口中的交互式按钮

ruby - 如何使用 mongoid/moped 进行 upsert/push

ruby-on-rails - 如何在 Rails 中正确使用基本作用域

url - 在 servlet 过滤器中排除特定 URL

html - 为什么图像的 URL 在 css 中不起作用?

ruby-on-rails - rails console - 运行一段代码

mysql - Rails Activerecord 关系 : using subquery as a table for a SQL join