Skip to content

Commit 4809dcc

Browse files
Cody Fauserjosh
authored andcommitted
* Remove default_url_options from mailer generator
* Improve mailer documentation regarding generating URLs * Add no_match to mailer generator to warn contributors about default_url_options Signed-off-by: Joshua Peek <josh@joshpeek.com>
1 parent 4ac33de commit 4809dcc

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

actionmailer/lib/action_mailer/base.rb

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,36 @@ module ActionMailer #:nodoc:
7373
# <%= truncate(note.body, 25) %>
7474
#
7575
#
76-
# = Generating URLs for mailer views
76+
# = Generating URLs
77+
#
78+
# URLs can be generated in mailer views using <tt>url_for</tt> or named routes.
79+
# Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request,
80+
# so you'll need to provide all of the details needed to generate a URL.
7781
#
78-
# If your view includes URLs from the application, you need to use url_for in the mailing method instead of the view.
79-
# Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request. That's
80-
# why you need to jump this little hoop and supply all the details needed for the URL. Example:
82+
# When using <tt>url_for</tt> you'll need to provide the <tt>:host</tt>, <tt>:controller</tt>, and <tt>:action</tt>:
83+
#
84+
# <%= url_for(:host => "example.com", :controller => "welcome", :action => "greeting") %>
8185
#
82-
# def signup_notification(recipient)
83-
# recipients recipient.email_address_with_name
84-
# from "system@example.com"
85-
# subject "New account information"
86-
# body :account => recipient,
87-
# :home_page => url_for(:host => "example.com", :controller => "welcome", :action => "greeting")
88-
# end
86+
# When using named routes you only need to supply the <tt>:host</tt>:
87+
#
88+
# <%= users_url(:host => "example.com") %>
89+
#
90+
# You will want to avoid using the <tt>name_of_route_path</tt> form of named routes because it doesn't make sense to
91+
# generate relative URLs in email messages.
92+
#
93+
# It is also possible to set a default host that will be used in all mailers by setting the <tt>:host</tt> option in
94+
# the <tt>ActionMailer::Base.default_url_options</tt> hash as follows:
95+
#
96+
# ActionMailer::Base.default_url_options[:host] = "example.com"
97+
#
98+
# This can also be set as a configuration option in <tt>environment.rb</tt>:
99+
#
100+
# config.action_mailer.default_url_options = { :host => "example.com" }
89101
#
90-
# You can now access @home_page in the template and get http://example.com/welcome/greeting.
102+
# If you do decide to set a default <tt>:host</tt> for your mailers you will want to use the
103+
# <tt>:only_path => false</tt> option when using <tt>url_for</tt>. This will ensure that absolute URLs are generated because
104+
# the <tt>url_for</tt> view helper will, by default, generate relative URLs when a <tt>:host</tt> option isn't
105+
# explicitly provided.
91106
#
92107
# = Sending mail
93108
#

railties/lib/rails_generator/generators/components/mailer/templates/mailer.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class <%= class_name %> < ActionMailer::Base
22
3-
# change to your domain name
4-
default_url_options[:host] = 'example.com'
53
<% for action in actions -%>
64

75
def <%= action %>(sent_at = Time.now)

railties/test/generators/rails_mailer_generator_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def test_generates_mailer
1717
],
1818
body.split("\n").map{|line| line.sub(' '*4, '') }
1919
end
20-
21-
assert_match /^ default_url_options\[:host\] = 'example.com'$/m, model,
22-
'model should include default_url_options :host declaration'
20+
21+
assert_no_match /(self.default_url_options =|default_url_options\[.*\] =)/, model,
22+
'individual mailer models should not set default_url_options because the options are shared by all mailers'
2323
end
2424

2525
assert_generated_views_for :notifier, 'reset_password.erb'

0 commit comments

Comments
 (0)