SEO/Rails - 如何将标题标签添加到每个"link_to"

Ale*_*oul 10 seo ruby-on-rails link-to

我很惊讶Rails的创造者没有想到这一点,如果有人可以提供帮助,那就太好了.

我们怎样才能改变这个:

<%= link_to "My Title", :controller => "products" %>
Run Code Online (Sandbox Code Playgroud)

这个自动:

<%= link_to "My Title", :controller => "products", :title => "My Title" #basically a copy of the text %>
Run Code Online (Sandbox Code Playgroud)

我认为它可以帮助SEO很多.

非常感谢!

亚历克斯

Abr*_*ram 13

这是rails 3的方式:

<%= link_to object_path, title: "Path Title" %>
Run Code Online (Sandbox Code Playgroud)

进一步阅读:https://www.searchenginejournal.com/how-to-use-link-title-attribute-correctly/


dsi*_*gnr 9

你的问题是有效的,我不知道为什么你被投票,但是,rails DID的创建者实际上是在想这个问题.实际上,您可以以非常简单的方式完成它,而不是使用自定义方法复杂化:

<%= link_to "Link", { :action => "show" }, { :title => "Title" } %>
Run Code Online (Sandbox Code Playgroud)

实际上,您可以添加任何您喜欢的参数,而不仅仅是标题.

希望这可以帮助!


cry*_*o28 -5

尝试类似的事情

def link_to_with_autotitle(title, args = {})
  link_to_without_autotitle(title, args.merge(:title => title))
end
alias_method_chain :link_to, :autotitle
Run Code Online (Sandbox Code Playgroud)

还没有测试过代码,也不记得确切的 link_to 规范,但我想你明白了