Including url_for/link_to In A Rails Mailer

3,587 views
Skip to first unread message

Bryce Thornton

unread,
Apr 12, 2010, 11:48:52 AM4/12/10
to delayed_job
I've been trying to get my rails mailer working with delayed_job and
seem to have hit a roadblock with "url_for" & "link_to". My mail
templates worked fine when they were being sent while processing the
web request. They include a few calls to url_for/link_to and now,
with delayed_job, the methods don't seem to be in scope. I've tried
the following:

require 'action_view/helpers/url_helper'

class UserMailer < ActionMailer::Base
include ActionView::Helpers::UrlHelper

helper_method :url_for
helper_method :link_to

def welcome_email(user)
...
end
end

That doesn't seem to work. What is the right way to deal with this?

Brandon Keepers

unread,
Apr 12, 2010, 12:30:12 PM4/12/10
to delay...@googlegroups.com

See the section on "Generating URLs": http://api.rubyonrails.org/classes/ActionMailer/Base.html


ActionMailer::Base.default_url_options[:host] = "example.com"


Brandon

Michael Guterl

unread,
Apr 12, 2010, 12:32:58 PM4/12/10
to delay...@googlegroups.com
I really like Ara Howard's initializer for setting this up without
much thought. I use it in all of my projects now.

http://drawohara.com/post/337775636/rails-relative-links-in-mailers-the-un-sucky-way

Best,
Michael Guterl

Bryce Thornton

unread,
Apr 12, 2010, 1:14:05 PM4/12/10
to delayed_job
Hmm. I already have this in my development.rb file:

config.action_mailer.default_url_options = { :host =>
'mydomain.local' }

The problem is not that the url's are relative vs. absolute, it's that
"url_for" is undefined in my mailer template's scope:

failed with ActionView::TemplateError: undefined method `url_for'

This did work when sending the mail inline. The issue only cropped up
when switching it to delayed_job.

On Apr 12, 12:32 pm, Michael Guterl <mgut...@gmail.com> wrote:

> http://drawohara.com/post/337775636/rails-relative-links-in-mailers-t...
>
> Best,
> Michael Guterl

Bryce Thornton

unread,
Apr 15, 2010, 10:12:21 AM4/15/10
to delayed_job
I finally figured this out. Here's what I had to use to get
everything in scope for delayed_job:

class UserMailer < ActionMailer::Base
helper ActionView::Helpers::UrlHelper

def welcome_email(user)
...
end
end

I was using "helper_method" instead of "helper". Makes a big
difference. http://apidock.com/rails/ActionMailer/Helpers/ClassMethods/helper

I guess rails put everything I needed into scope for me when sending
mail during the response. Just figured I'd post my findings for the
next guy who has this issue.

Kevin

unread,
May 11, 2010, 12:49:11 PM5/11/10
to delayed_job
Thanks for the help. This saved me hours. I also had to add:

helper ActionView::Helpers::UrlHelper

On Apr 15, 9:12 am, Bryce Thornton <brycethorn...@gmail.com> wrote:
> I finally figured this out.  Here's what I had to use to get
> everything in scope for delayed_job:
>
> class UserMailer < ActionMailer::Base
>   helper ActionView::Helpers::UrlHelper
>
>   def welcome_email(user)
>     ...
>   end
> end
>
> I was using "helper_method" instead of "helper".  Makes a big
> difference.http://apidock.com/rails/ActionMailer/Helpers/ClassMethods/helper
Reply all
Reply to author
Forward
0 new messages