Forum: Ruby on Rails link_to method throws a url_for error

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.
Jason F. (Guest)
on 2007-01-03 01:00
(Received via mailing list)
This error seems pretty bizarre. I'm using ActionMailer and am using
a template in app/views/welcome_mailer/join.rhtml which has this line:

<%= link_to :controller => 'group', :action => 'join', :group =>
@group.id, :ic => @code.code %>

The error is: undefined method `url_for' for #<WelcomeMailer:
0x2aaaad4fb470>
I know the variables are OK because I've tested them. If I change the
link_to to a url_for, I get the exact same error.

Anyone know what's the problem? All help is appreciated & thanks in
advance!

-Jason
Dan M. (Guest)
on 2007-01-03 07:31
(Received via mailing list)
Jason F. wrote:
> The error is: undefined method `url_for' for #<WelcomeMailer:
> 0x2aaaad4fb470>
>
> Anyone know what's the problem? All help is appreciated & thanks in
> advance!

The problem is that the mailer does not have a controller to reference,
which is needed for url_for.  One way to solve this is by passing the
controller to the mailer:

def my_mailer_action(controller, *rest)
  @body[:controller] = controller
end

Then you should be able to use url_for.  The only problem with this
solution is if you are not sending e-mail from a controller.

Dan M.
Jason F. (Guest)
on 2007-01-03 20:15
(Received via mailing list)
On Jan 2, 2007, at 9:30 PM, Dan M. wrote:

> which is needed for url_for.  One way to solve this is by passing the
>
Thanks for your reply Dan. It turns out I'm not using a controller: I
only have a welcome_mailer model with a join method in it, and then
the join.rhtml template with the link_to in it (I'm assuming that if
I add your code into the model it won't work). I also tried putting
"helper ActionView::Helpers::UrlHelper" inside my WelcomeMailer class
but that didn't seem to help either.

If anyone has another idea for how to get around this, please let me
know.

Thanks again, in advance!
-Jason
This topic is locked and can not be replied to.