Search code examples
ruby-on-railsrack-pow

How to generate URL with host in Rails development environment?


I am working on an application that has set its default host to localhost:

config.action_controller.default_url_options = { host: "localhost" }
config.action_mailer.default_url_options = { host: "localhost" }

This works fine if you're serving your app on a specific port (like http://localhost:3000). I am using Pow so that I can access the app from a URL like http://myapp.dev.

How can I change this setting so that it will work with my domain as well as the other developers using localhost? I need to generate full URLs since they will be used in emails. Is it possible to pass some sort of config value to Pow?


Solution

  • You could use an environment variable to configure a default host. See Luc Boissaye's answer on how to set this up with dotenv. If you don't add the .env to your repo, each developer can create his own .env and configure his (or her) own preferences.

    But you can also use existing environment variables to configure the default_url_options. For example Pow.cx sets the POW_DOMAINS variable:

    config.action_mailer.default_url_options = {
      ENV['POW_DOMAINS'].present? ? 'my-app.dev' : 'localhost' 
    }
    

    As far as I know you only need to set the config.action_controller.default_url_options if you want to force it to some default. When you use the rails _path helpers in your views, this will generate a relative URL (/path). If you use the _url helpers, this will generate an absolute URL (http://your.host/path), but both the protocol (http) and host (your.host) are request based.