Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate correct URLs in mailer templates?

I am using Ruby on Rails 3.1.0 and I would like to properly generate URLs in HTML email messages. In my environment file I set

config.action_mailer.default_url_options = { :host => 'my_site.org' }

In the email view file (.html.erb) I state

<%= link_to @user.name, users_url(@user) %>

When I go to see the received email the generated URL is http://users/1, of course no correct. So, how can I generate correct URLs in mailer templates so to have http://my_site.org/users/1 links in body messages?


I also tryed to set the default_url_options in my mailer.rb file

class MyCustom::Mailer < ActionMailer::Base
  default_url_options[:host] = 'my_site.org'

  def test_sending
    ...
  end
end

but it doesn't work.

like image 818
user502052 Avatar asked Dec 28 '22 08:12

user502052


2 Answers

users_path is the relative path (/users/1). For an email, you want the absolute path, so use users_url(@user), which will give http://myapp.com/users/1 instead.

like image 76
MrTheWalrus Avatar answered Jan 09 '23 14:01

MrTheWalrus


your action_mailer setting is correct.

But you should be using _url and not _path for the link_to,

<%= link_to @user.name, users_url(@user) %>
like image 20
John Beynon Avatar answered Jan 09 '23 14:01

John Beynon