Helper methods available in mailer views depend on environment?

[亡魂溺海] 提交于 2019-12-11 07:37:12

问题


Related to my previous question I still have one thing that I would like to understand - why this:

= link_to(root_path)
= link_to(@some_path_set_in_mailer)

works in development mode (config.action_mailer.perform_deliveries was set to true and emails were actually sent) and in production or staging has to be changed to:

= link_to(@some_path_set_in_mailer, @some_path_set_in_mailer)

to avoid "No route matches {}" error?

I had this problem in rails 3.2.


回答1:


I'm not entirely sure why there is a difference since there shouldn't be.

However, link_to typically has this format:

= link_to("Some link description here", root_path)

The only time you typically leave off the link description text is if you have a longer description that you need to put within a do block like this:

= link_to(root_path) do
  %p Some link description here
  = image_tag("some_image.jpg")

So I would recommend sticking to the preferred syntaxes above.

The docs for link_to don't really talk about the short-hand method you're using too much.

Here is the source for it:

# File actionpack/lib/action_view/helpers/url_helper.rb, line 231
def link_to(*args, &block)
  if block_given?
    options      = args.first || {}
    html_options = args.second
    link_to(capture(&block), options, html_options)
  else
    name         = args[0]
    # this is probably where your issue is coming from
    options      = args[1] || {}
    html_options = args[2]

    html_options = convert_options_to_data_attributes(options, html_options)
    url = url_for(options)

    href = html_options['href']
    tag_options = tag_options(html_options)

    href_attr = "href=\"#{ERB::Util.html_escape(url)}\"" unless href
    "<a #{href_attr}#{tag_options}>#{ERB::Util.html_escape(name || url)}</a>".html_safe
  end
end


来源:https://stackoverflow.com/questions/9039970/helper-methods-available-in-mailer-views-depend-on-environment

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