rails4多站点在视图中显示主机 – 有时可行

现在整个下午一直在努力……

我在heroku上有一个代码库多个应用程序设置。 我试图在我的主域上运行的主页(stati_controller)上显示主机:www.domain.hu(env:production,production.rb)但不在另一个主页上:www.domain.co。 uk(env:production-uk,production-uk.rb)Debug告诉我:nil我缺少什么?

基于此 ,我尝试了以下但没有运气(一个接一个地尝试……)。 在production-uk.rb中

Rails.application.default_url_options[:host] = 'www.domain.co.uk' Rails.application.default_url_options[:host] = 'domain.co.uk' config.asset_host = 'www.domain.co.uk' - this "kills" the CSS config.default_url_options = { host: 'www.domain.co.uk' } config.default_url_options[:host] = 'domain.co.uk' config.action_controller.default_url_options = { host: 'www.domain.co.uk' } config.action_controller.default_url_options = { host: 'domain.co.uk' } Rails.application.default_url_options = { host: 'domain.co.uk' } config.action_controller.default_url_options = { host: 'domain.co.uk' } config.action_controller.default_url_options[:host] = 'domain.co.uk' host = 'www.domain.co.uk' config.action_controller.default_url_options = { host: host } 

我也在static_pages_controller.rb中试过这个 – 没有运气(有或没有前置filter):

  def default_url_options if Rails.env == 'production' {:host => "www.domain.hu"} elsif Rails.env == 'production-uk' {:host => "www.domain.co.uk"} else {} end end 

validation基于request.host的工作正常,所以我不明白(static_pages_controller.rb):

 before_filter :authenticate ... def authenticate domain = request.host if domain == 'www.domain.hu' authenticate_or_request_with_http_basic do |username, password| username == 'stuff' && password == 'boda' end elsif domain == 'www.domain.co.uk' authenticate_or_request_with_http_basic do |username, password| username == 'stuff' && password == 'boda' end end end 

我在生产中也有这个 – uk.rb也可以:

 host = 'www.domain.co.uk' config.action_mailer.default_url_options = { host: host } 

更新还有一件事:当我在应用程序的heroku控制台并编写app.host时,回复总是“www.example.com”

谢谢阅读!

您可以尝试在每个heroku应用程序上设置域名

 heroku config:set DOMAIN_NAME=www.domain.co.uk 

在你的应用程序中,你打电话

 ENV['DOMAIN_NAME'] 

这是一个严重的错误。 我有

 @host = request.host 

在static_pages_controller.rb中的gmaps4rails位内

这就是为什么它并不总是有效…. grr