如何在rails中设置url助手的默认主机?
我想做这样的事情
config.default_host = 'www.subdomain.example.com'  在我的一些configuration文件中,这样object_url助手( ActionView::Helpers::UrlHelper )产生以http://www.subdomain.example.com开头的链接 
 我试图search文档,但我没有find除ActionMailer文档和http://api.rubyonrails.org/classes/Rails/Configuration.html这是没有用的,因为我不知道在哪个轻拍看。 有没有一个描述Rails :: Initializer.config整个结构的地方? 
  asset_host不适用于url 
 您需要覆盖ApplicationController default_url_options (至less在Rails 3中) 
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 
在环境configuration中定义默认主机:
 # config/environments/staging.rb MyApp::Application.configure do # ... Rails.application.routes.default_url_options[:host] = 'preview.mydomain.com' # ... end 
然后你可以在你的应用程序的任何地方创build一个URL
 Rails.application.routes.url_helpers.widgets_url() 
或者在你的课堂中包含URL助手:
 class MyLib include Rails.application.routes.url_helpers def make_a_url widgets_url end end 
如果您未定义默认主机,则需要将其作为选项进行传递:
 widgets_url host: (Rails.env.staging? ? 'preview.mydomain.com' : 'www.mydomain.com') 
指定协议之类的东西也很有用:
 widgets_url protocol: 'https' 
另一种方法是像这样设置
 # config/production.rb config.action_controller.default_url_options = { host: 'myproduction.com' } 
 据我所知, *_url助手使用服务器configuration的主机名。 所以例如,如果我的Apache安装在http://www.myapp.com/接受这个Rails应用程序的请求,那么Rails将使用这个地址。 这就是为什么开发环境中的*_url方法默认指向http://localhost:3000的原因。 
 在上一个答案中build议的资产主机只会影响image_tag , stylesheet_link_tag和javascript_link_tag帮助程序。 
最简单的方法来获取您的dynamic域名在轨道中使用全局variables。
 class ApplicationController < ActionController::Base def base_url $base_url = request.base_url end end 
 在调用这个方法到你的头文件<% base_url %> ,现在你可以在你的应用程序的任何地方访问你的域名。 
 您可以轻松地为每个url_helper设置:host或/ and :only_path参数。  yours_url(params, :host => "http://example.com", :only_path => Rails.env.test?)这样你就不会在你的环境中设置全局的default_url_options,除非你想要。 
NSD的解决scheme是我如何做,但我不得不添加一个块,使其与https:
 config.action_controller.asset_host = Proc.new { |source, request| (request ? request.protocol : 'http://') + "www.subdomain.example.com" } 
有这个,但我不是非常确定他们是否是你指的助手:
 ActionController::Base.asset_host = "assets.example.com" 
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html