From 5dccb094eb77f4c082c85e2071e12c3cea527f12 Mon Sep 17 00:00:00 2001 From: Cody Fauser Date: Mon, 21 Apr 2008 10:11:12 -0400 Subject: [PATCH] Remove default_url_options from mailer generator. Improve mailer documentation regarding generating URLs --- actionmailer/lib/action_mailer/base.rb | 39 ++++++++++++++------ .../components/mailer/templates/mailer.rb | 2 - .../test/generators/rails_mailer_generator_test.rb | 3 -- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 8a4f344..3dfe828 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -73,21 +73,36 @@ module ActionMailer #:nodoc: # <%= truncate(note.body, 25) %> # # - # = Generating URLs for mailer views + # = Generating URLs + # + # URLs can be generated in mailer views using url_for or named routes. + # Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request, + # so you'll need to provide all of the details needed to generate a URL. # - # If your view includes URLs from the application, you need to use url_for in the mailing method instead of the view. - # Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request. That's - # why you need to jump this little hoop and supply all the details needed for the URL. Example: + # When using url_for you'll need to provide the :host, :controller, and :action: + # + # <%= url_for(:host => "example.com", :controller => "welcome", :action => "greeting") %> # - # def signup_notification(recipient) - # recipients recipient.email_address_with_name - # from "system@example.com" - # subject "New account information" - # body :account => recipient, - # :home_page => url_for(:host => "example.com", :controller => "welcome", :action => "greeting") - # end + # When using named routes you only need to supply the :host: + # + # <%= users_url(:host => "example.com") %> + # + # You will want to avoid using the name_of_route_path form of named routes because it doesn't make sense to + # generate relative URLs in email messages. + # + # It is also possible to set a default host that will be used in all mailers by setting the :host option in + # the ActionMailer::Base.default_url_options hash as follows: + # + # ActionMailer::Base.default_url_options[:host] = "example.com" + # + # This can also be set as a configuration option in environment.rb: + # + # config.action_mailer.default_url_options = { :host => "example.com" } # - # You can now access @home_page in the template and get http://example.com/welcome/greeting. + # If you do decide to set a default :host for your mailers you will want to use the + # :only_path => false option when using url_for. This will ensure that absolute URLs are generated because + # the url_for view helper will, by default, generate relative URLs when a :host option isn't + # explicitly provided. # # = Sending mail # diff --git a/railties/lib/rails_generator/generators/components/mailer/templates/mailer.rb b/railties/lib/rails_generator/generators/components/mailer/templates/mailer.rb index 0c7e6be..ce15ae9 100644 --- a/railties/lib/rails_generator/generators/components/mailer/templates/mailer.rb +++ b/railties/lib/rails_generator/generators/components/mailer/templates/mailer.rb @@ -1,7 +1,5 @@ class <%= class_name %> < ActionMailer::Base - # change to your domain name - default_url_options[:host] = 'example.com' <% for action in actions -%> def <%= action %>(sent_at = Time.now) diff --git a/railties/test/generators/rails_mailer_generator_test.rb b/railties/test/generators/rails_mailer_generator_test.rb index 03170c3..dc0095d 100644 --- a/railties/test/generators/rails_mailer_generator_test.rb +++ b/railties/test/generators/rails_mailer_generator_test.rb @@ -17,9 +17,6 @@ class RailsMailerGeneratorTest < GeneratorTestCase ], body.split("\n").map{|line| line.sub(' '*4, '') } end - - assert_match /^ default_url_options\[:host\] = 'example.com'$/m, model, - 'model should include default_url_options :host declaration' end assert_generated_views_for :notifier, 'reset_password.erb' -- 1.5.3.7