logo
  • Jobs
  • About Me
  • Contact
  • Home
« Lazy Web Request: Google Reader and comments
CMD-K for searching in Safari »

Generating URLs with ActionMailer

Posted April 9th, 2008 by Matt Berther

ActionMailer is a very handy class for sending emails from your rails application. However, the problem that typically arises is the sending of links to your site via rails using the url_for method. Acording to the API documentation, this is because the mailer instance does not have any context about the incoming request. The API documentation recommends passing the host to the url_for parameter, like this:

url_for(:host => "www.myproductionsite.com", :controller => :home, :action => :index)

However, when using multiple url_for calls in a model, this can lead to a violation of DRY. Therefore, the typical resolution to this has been to add some declarations to your class, like this:

class Notification < ActionMailer::Base
  default_url_options[:host] = "www.myproductionsite.com"
end

I dont really care for this approach, as it locks my model into operating under the assumptions of a particular environment. I am unable to verify the links in my development or test environments without some string manipulation. A little effort led me to this approach, which so far seems to be much better. Instead of declaring the default_url_options in the ActionMailer model, I’m including them in config/environments/*.rb in this way.

# development.rb and test.rb
class ActionMailer::Base
  default_url_options[:host] = "localhost"
  default_url_options[:port] = 3000
end
 
# production.rb
class ActionMailer::Base
  default_url_options[:host] = "www.myproductionsite.com"
end

I am taking advantage of the environments file to give me the benefits of custom url options based on the environment that I am currently executing in. This is precisely what these files were designed for.

This entry was posted on Wednesday, April 9th, 2008 at 10:09 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

John Trupiano
July 29th, 2008

Helpful hint– I know we’ve struggled with this in the past. Such a simple solution…

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

Social
  • mattberther on twitter
Syndication
Archives
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
Jobs
mattberther.com © 2003 - 2008