# Link\_to method throws a url\_for error

**URL:** <https://www.ruby-forum.com/t/link-to-method-throws-a-url-for-error/81004>\
**Category:** Rails\
**Created:** [January 3, 2007, 12:00am UTC](https://www.ruby-forum.com/t/link-to-method-throws-a-url-for-error/81004 "2007-01-03T00:00:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [January 3, 2007, 12:00am UTC](https://www.ruby-forum.com/t/link-to-method-throws-a-url-for-error/81004/1 "2007-01-03T00:00:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [January 3, 2007, 6:31am UTC](https://www.ruby-forum.com/t/link-to-method-throws-a-url-for-error/81004/2 "2007-01-03T06:31:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [January 3, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/link-to-method-throws-a-url-for-error/81004/3 "2007-01-03T19:15:53Z")

</div>

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
