1

与我之前的问题相关,我仍然想了解一件事 - 为什么会这样:

= link_to(root_path)
= link_to(@some_path_set_in_mailer)

在开发模式下工作(config.action_mailer.perform_deliveries 设置为 true 并且电子邮件已实际发送)并且在生产或登台中必须更改为:

= link_to(@some_path_set_in_mailer, @some_path_set_in_mailer)

避免“没有路线匹配{}”错误?

我在rails 3.2中遇到了这个问题。

4

1 回答 1

0

我不完全确定为什么会有差异,因为不应该有。

但是,link_to通常具有以下格式:

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

通常,您唯一不使用链接描述文本的情况是,如果您需要将更长的描述放在do这样的块中:

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

所以我建议坚持上面的首选语法。

的文档link_to并没有真正谈论您使用太多的速记方法。

这是它的来源:

# 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
于 2012-01-27T22:34:22.487 回答