link_to method throws a url_for error

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

Jason Frankovitz 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 Manges

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