Question

I have an application (rails 3.2) with haml (3.1.4) emails. In the email template I wanted to use link_to, but apparently none of those 4 is working in production:

#{link_to(my_models_url)}
= link_to(my_models_url)

/ @url set to my_models_url
#{link_to(@url)}
= link_to(@url)

On development mode everything works fine, but in production, I keep getting the following error:

No route matches {}
 actionpack (3.2.0) lib/action_dispatch/routing/route_set.rb:503:in `raise_routing_error'

It works when I use helper methods before:

/ @my_link set to link_to(my_models_url)
#{@my_link}
= @my_link

But this is not convenient, if there are more links in the email and in general I don't see why any of the first 4 options should not be ok. I have no idea where is this problem comming from. I would appreciate any help on this...

SOLUTION:

Thanks to iWasRobbed I found where my problems were:

  1. all {resource}_path and {resource}_url have to be set in mailers as @variables, they are not available in mailer views
  2. apparently link_to() method in mailer is not the same as in rails views... it always needs 2 arguments, so instead of link_to(@link) available in views, one needs to do link_to(@link,@link). Pffff...
Was it helpful?

Solution

You need to declare the URL within the mailer.rb file. This isn't a HAML issue so much as it's just the way ActionMailer was designed.

def some_mailer_here
  @url = my_models_url
end

Then in your mailer view you can do:

= link_to("My models URL", @url)

http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top