我不完全确定为什么会有差异,因为不应该有。
但是,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