[Ruby] ApplicationHelper methods in ActionMailer templates

Scott Windsor swindsor at gmail.com
Thu Jan 3 17:56:46 PST 2008


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

Here's the documentation for UrlWriter that shows how to use this.
http://api.rubyonrails.org/classes/ActionController/UrlWriter.html

- scott

On Jan 3, 2008, at 5:40 PM, Ken Harris wrote:

> (Ruby 1.8.6, Rails 1.2.5.)
>
> How do you use/get ApplicationHelper methods in an ActionMailer
> template?
>
> The comment above ApplicationHelper says it's included in all
> templates.  I've got a method in ApplicationHelper that wraps url_for
> (for a specific case), and I'm using in my normal Rails views, and
> everything is good.
>
> I want to put this link in an email, too, so I tried just using the
> helper in my ActionMailer template.  No good: apparently
> ApplicationHelper is only included in templates called by
> ActionController or something.  No problem -- I'll just include
> ApplicationHelper where I need it by hand, right?
>
> Well, after mucking around for a while, I can't seem to figure it out.
> The method doesn't seem to exist even when explicitly imported, or the
> url_for call is expecting something which ActionMailer::Base doesn't
> have, or something worse.
>
> The obvious things didn't work, and the more obscure solutions I try
> to come up with, the weirder the error messages get.  That's usually a
> sign I'm missing something simple and correct.
>
> Has anybody done this before?  What stupidly simple thing am I
> missing?
>
> thanks,
>
>
> - Ken
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby



More information about the Ruby mailing list