[Ruby] ApplicationHelper methods in ActionMailer templates

Alex Vollmer alex.vollmer at gmail.com
Thu Jan 3 18:42:42 PST 2008


On Jan 3, 2008, at Jan 3, 5:56 PM, Scott Windsor wrote:

> You need to include the ActionController::UrlWriter in the
> ActionMailer::Base class.  But, url_for won't know what host/port
> your application is running on, so you'll have to set the defaults.
>
> # add this in your environments.rb for Rails 1.x or in an initializor
> for Rails 2.x
> class ActionMailer::Base
>   include ActionController::UrlWriter
>   default_url_options[:host] = 'myhostname.com'
>   default_url_options[:port] = 3000
> end


If your mail is getting kicked off by a controller action, another way  
to do it is to take advantage of the host/port info that's available  
during request dispatching. You can do something like this:

class UsersController < Application
   before_filter :setup_mailer_defaults

   ...

   def setup_mailer_defaults
     ActionMailer::Base.default_url_options[:host] =  
request.host_with_port
   end
end

This will set the host and port to match your running environment so  
you don't have to fiddle with setting up the ActionMailer::Base  
differently for each environment.

Cheers,

Alex V.

----
Musings & Notes
http://blog.livollmers.net





More information about the Ruby mailing list