rails4 multisite show host in view - sometimes wor

2019-09-12 17:06发布

问题:

Have been working on this all afternoon now...

I have a one codebase multiple apps setup on heroku. I am trying to display the host on the home page (stati_controller) which works on my main domain: www.domain.hu (env: production, production.rb) but doesn't on the other one: www.domain.co.uk (env: production-uk, production-uk.rb) Debug tells me: nil What am I missing?

Based on this and this I tried the followings but no luck (tried one-by-one...). In 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 }

I also tried this in static_pages_controller.rb - no luck (with or without before-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 

And authenticate works based on request.host fine so I don't understand (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

I have this in production-uk.rb too that works:

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

UPDATE There is one more thing: When I am in the heroku console of the app and write app.host the reply is always "www.example.com"

Thanks for reading!

回答1:

You can try to set the domain name on each heroku app like

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

and in your app you call

ENV['DOMAIN_NAME']


回答2:

It was a stvpid mistake. I had

@host = request.host

inside the gmaps4rails's bit in the static_pages_controller.rb

That's why it didn't always work.... grr