rails4 multisite show host in view - 有时有效

rails4 multisite show host in view - sometimes works

整个下午都在研究这个...

我在 heroku 上设置了一个代码库多个应用程序。我试图在主页 (stati_controller) 上显示主机,该主机在我的主域上运行:www.domain.hu (env: production, production.rb) 但在另一个域上不运行: www.domain.co.uk(环境:英国生产,生产-uk.rb) 调试告诉我:无 我错过了什么?

基于this and this我尝试了以下方法但没有成功(一个一个地尝试......)。在生产-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 中尝试过这个 - 运气不好(有或没有前置过滤器):

  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 

验证工作基于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