Ruby-on-rails 如何在 rails 中为 url helper 设置默认主机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2660172/
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
How do I set default host for url helpers in rails?
提问by gorn
I would like to do something like this
我想做这样的事情
config.default_host = 'www.subdomain.example.com'
in some of my configuration files, so that object_urlhelpers (ActionView::Helpers::UrlHelper) produce links beginning with http://www.subdomain.example.com
在我的一些配置文件中,以便object_url助手 ( ActionView::Helpers::UrlHelper) 生成以http://www.subdomain.example.com开头的链接
I have tried to search the docs but I did not find anything except ActionMailerdocs and http://api.rubyonrails.org/classes/Rails/Configuration.htmlwhich is not useful for me, because I do not know in which pat to look. Is there a place which describes the whole structure of Rails::Initializer.config?
我试图搜索文档,但除了ActionMailer文档和http://api.rubyonrails.org/classes/Rails/Configuration.html之外我什么也没找到,这对我没有用,因为我不知道在哪个 pat . 有没有地方描述 Rails::Initializer.config 的整个结构?
采纳答案by Wilhelm
asset_hostdoesn't work for urls
asset_host不适用于网址
You need to override default_url_optionsin your ApplicationController(at least in Rails 3)
你需要重写default_url_options你的ApplicationController(至少在Rails 3中)
http://edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options
http://edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options
class ApplicationController < ActionController::Base
def default_url_options
if Rails.env.production?
{:host => "myproduction.com"}
else
{}
end
end
end
回答by Andrew
Define the default host in your environment config:
在您的环境配置中定义默认主机:
# config/environments/staging.rb
MyApp::Application.configure do
# ...
Rails.application.routes.default_url_options[:host] = 'preview.mydomain.com'
# ...
end
Then you can create a URL anywhere in your app:
然后你可以在你的应用程序的任何地方创建一个 URL:
Rails.application.routes.url_helpers.widgets_url()
Or include the URL helpers in your class:
或者在你的类中包含 URL 助手:
class MyLib
include Rails.application.routes.url_helpers
def make_a_url
widgets_url
end
end
If you don't define the default host, you will need to pass it as an option:
如果您没有定义默认主机,则需要将其作为选项传递:
widgets_url host: (Rails.env.staging? ? 'preview.mydomain.com' : 'www.mydomain.com')
It's also useful to specify things like the protocol:
指定协议之类的内容也很有用:
widgets_url protocol: 'https'
回答by goma
Another way is to set it like this
另一种方法是这样设置
# config/production.rb
config.action_controller.default_url_options = { host: 'myproduction.com' }
回答by NStojanovic
You can easily set :hostor/and :only_pathparameter for every url_helper.
yours_url(params, :host => "http://example.com", :only_path => Rails.env.test?)
This way you are not setting global default_url_options in your environments, unless you want that.
您可以轻松地为每个 url_helper设置:host或/和:only_path参数。
yours_url(params, :host => "http://example.com", :only_path => Rails.env.test?)
这样你就不会在你的环境中设置全局 default_url_options,除非你想要。
回答by Robert Speicher
As far as I know, the *_urlhelpers use the server's configured host name. So for example if my Apache installation is accepting requests for this Rails app at http://www.myapp.com/then Rails will use that address. That's why the *_urlmethods in a development environment point to http://localhost:3000by default.
据我所知,*_url助手使用服务器配置的主机名。例如,如果我的 Apache 安装正在接受对这个 Rails 应用程序的请求,http://www.myapp.com/那么 Rails 将使用该地址。这就是为什么*_url开发环境中的方法http://localhost:3000默认指向的原因。
The asset host suggested in the previous answer will only affect the image_tag, stylesheet_link_tagand javascript_link_taghelpers.
上一个答案中建议的资产主机只会影响image_tag,stylesheet_link_tag和javascript_link_tag助手。
回答by prasanthrubyist
Simplest way to get your dynamic domain url using a Global variable in rails.
使用 rails 中的全局变量获取动态域 url 的最简单方法。
class ApplicationController < ActionController::Base
def base_url
$base_url = request.base_url
end
end
After just call this method into your header file <% base_url %>, Now you can access your domain url anywhere in your application.
只需将此方法调用到您的头文件中<% base_url %>,现在您就可以在应用程序的任何位置访问您的域 url。
回答by simianarmy
NSD's solution is how I do it, but I had to add a block to make it work with https:
NSD 的解决方案是我的方法,但我必须添加一个块才能使其与 https 一起使用:
config.action_controller.asset_host = Proc.new { |source, request|
(request ? request.protocol : 'http://') + "www.subdomain.example.com"
}
回答by Azeem.Butt
There's this, but I'm not terribly sure if they're the helpers you're referring to:
有这个,但我不太确定他们是否是您所指的帮手:
ActionController::Base.asset_host = "assets.example.com"
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html

