如何在 rails 中为 url helper 设置默认主机? [英] How do I set default host for url helpers in rails?

查看:28
本文介绍了如何在 rails 中为 url helper 设置默认主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情

config.default_host = 'www.subdomain.example.com'

在我的一些配置文件中,以便 object_url 助手 (ActionView::Helpers::UrlHelper) 生成以 http://www.subdomain.example.com

in some of my configuration files, so that object_url helpers (ActionView::Helpers::UrlHelper) produce links beginning with http://www.subdomain.example.com

我尝试搜索文档,但除了 ActionMailer 文档和 http://api.rubyonrails.org/classes/Rails/Configuration.html 这对我没有用,因为我不知道在哪个 pat 看.有没有介绍Rails::Initializer.config 的整个结构的地方?

I have tried to search the docs but I did not find anything except ActionMailer docs and http://api.rubyonrails.org/classes/Rails/Configuration.html which 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?

推荐答案

asset_host 对 url 不起作用

asset_host doesn't work for urls

您需要在 ApplicationController 中覆盖 default_url_options(至少在 Rails 3 中)

You need to override default_url_options in your ApplicationController (at least in Rails 3)

http://edgeguides.rubyonrails.org/action_controller_overview.html#default-网址选项

class ApplicationController < ActionController::Base
  def default_url_options
    if Rails.env.production?
      {:host => "myproduction.com"}
    else  
      {}
    end
  end
end

这篇关于如何在 rails 中为 url helper 设置默认主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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