2

在 Rails 3 中,使用methoda 的属性时link_to,我的自定义rel值被覆盖。

# my code:
link_to 'Add to Favorites',
  profile_favorites_path(profile),
  :method => :post,
  :class => 'button',
  :rel => 'favorite'

# expected result:
<a href="/profiles/1/favorites" class="button" data-method="post" rel="favorite nofollow">Add to Favorites</a>

# actual result:
<a href="/profiles/1/favorites" class="button" data-method="post" rel="nofollow">Add to Favorites</a>

这是 Rails 错误/意外功能吗?如何在method提供自定义值的同时利用内置功能rel

4

1 回答 1

4

如果设置为获取,似乎您只能指定rel属性。method

在里面action_view/helpers/url_helper.rb add_method_to_attributes!你可以看到当前正在使用的逻辑轨:

def add_method_to_attributes!(html_options, method)
  html_options["rel"] = "nofollow" if method && method.to_s.downcase != "get"
  html_options["data-method"] = method if method
end

这实际上是有道理的,您不希望在抓取链接时让机器人/蜘蛛发布。

于 2011-06-21T04:23:42.610 回答