Forum: Ruby on Rails ActionMailer and url_for in helper methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Conrad C. (Guest)
on 2009-01-18 22:57
Hi,

I was wondering how to get url_for to work inside ActionMailer.  I did
research on the web but I can't seem to find something that fits what I
need.

In my application, each user can specify their custom domain to access
their.  I have an ActionMailer that sends them notifications of changes
that occur.  In there, I have a breadcrumbs helper method, which
generages something like:

<a href="/page/1">1</a> &gt;  <a href="/page/2">2</a>

I need it to be:

<a href="http://customdomain/page/1">1</a> &gt;  <a
href="http://customdomain/page/2">2</a>

How do you that?  I tried using default_url_options but it doesn't
affect the helper methods I'm using in the email.  Can somebody help me?

Thanks!
Conrad

----

Here's an excerpt of my code:

class UserNotifier < ActionMailer::Base
  helper :application

    # Emails the user the recent changes on the wiki
  def changes(user, page)
    ...
    default_url_options[:host] = user.custom_host
    @body => {"page" => page}
    ...
  end
end

views/user_notifer/changes.rhtml:
<%= breadcrumbs(@page) %>
Ryan W. (Guest)
on 2009-01-19 06:13
(Received via mailing list)
Use the :host option to make it build the whole URL, like this:

url_for( {:host =>'customerdomain.com', :controller => 'page', :id => 1
} )

On Sun, Jan 18, 2009 at 2:57 PM, Conrad C. <
Conrad C. (Guest)
on 2009-01-19 21:25
Unfortunately, my email is using helper methods that are also used
throughout the application, so I don't want to hardcode or pass around a
host variable everywhere (because there are a lot of URLs being
generated).  I thought the ActionMailer::Base.default_url_options[:host]
would help but it doesn't seem to work inside the scope of helper
methods.

Ryan W. wrote:
> Use the :host option to make it build the whole URL, like this:
>
> url_for( {:host =>'customerdomain.com', :controller => 'page', :id => 1
> } )
>
> On Sun, Jan 18, 2009 at 2:57 PM, Conrad C. <
Ryan W. (Guest)
on 2009-01-20 00:36
(Received via mailing list)
Perhaps this would be of use:

http://www.mattberther.com/2008/04/09/generating-u...

You may also need to pass ':only_path => false', since this was at one
point
a requirement with link_to:

http://dev.rubyonrails.org/ticket/11446

This is also fun:

http://pivotallabs.com/users/nick/blog/articles/28...

HTH!

On Mon, Jan 19, 2009 at 1:25 PM, Conrad C. <
This topic is locked and can not be replied to.