rails 3.2 haml email template link_to error

社会主义新天地 提交于 2019-12-11 07:36:49

问题


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...

回答1:


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



来源:https://stackoverflow.com/questions/9038917/rails-3-2-haml-email-template-link-to-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!