Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 ActionMailer subdomain urls

When a rails 4 application is hosted on a subdomain e.g. sub.domain.com, how can you get urls in Action Mailer templates to link to the subdomain correctly?

config/environments/production.rb:

config.action_mailer.default_url_options = { :host => 'sub.domain.com' }

action mailer template link example:

<%= user_url(@user) %>

In the email, the link shows as www.domain.com/users/1 rather then sub.domain.com/users/1

like image 463
Nicholas.V Avatar asked Dec 20 '22 23:12

Nicholas.V


2 Answers

It’s actually easy. The best suggestion I have for solving this problem is that you create a before_filter that sets it on each request in ApplicationController.rb like so:

before_filter :set_mailer_host

  def set_mailer_host
    ActionMailer::Base.default_url_options[:host] = request.host_with_port
  end
like image 72
jimagic Avatar answered Jan 22 '23 17:01

jimagic


I doubt this is still an issue for you, but I thought I'd add something helpful I found related to this.

Aside from adding your default_url_options in the various config files (be sure that you add that in all environments you need it), you will want to get the whole url, not just the path.

<%= url_for() %>

You can specify a subdomain or domain as a parameter, as well as several other options (from apidock):

url_for(options = nil) public Generate a url based on the options provided, default_url_options and the routes defined in routes.rb. The following options are supported:

:only_path - If true, the relative url is returned. Defaults to false.

:protocol - The protocol to connect to. Defaults to ‘http’.

:host - Specifies the host the link should be targeted at. If :only_path is false, this option must be provided either explicitly, or via default_url_options.

:subdomain - Specifies the subdomain of the link, using the tld_length to split the subdomain from the host. If false, removes all subdomains from the host part of the link.

:domain - Specifies the domain of the link, using the tld_length to split the domain from the host.

:tld_length - Number of labels the TLD id composed of, only used if :subdomain or :domain are supplied. Defaults to ActionDispatch::Http::URL.tld_length, which in turn defaults to 1.

:port - Optionally specify the port to connect to.

:anchor - An anchor name to be appended to the path.

:trailing_slash - If true, adds a trailing slash, as in “/archive/2009/”

:script_name - Specifies application path relative to domain root. If provided, prepends application path.

Any other key (:controller, :action, etc.) given to url_for is forwarded to the Routes module.

I wound up using link_to with the "_url" end to the route. Like this:

<%= link_to 'Yes', response_approvals_url(t: @secret_token) %>
like image 22
richardun Avatar answered Jan 22 '23 16:01

richardun