我很惊讶Rails的创造者没有考虑到这一点,如果有人能帮忙,那就太好了。
我们应该怎么做才能改变这个呢:
<%= link_to "My Title", :controller => "products" %>
自动执行此操作:
<%= link_to "My Title", :controller => "products", :title => "My Title" #basically a copy of the text %>
我认为这可以大大帮助SEO。
非常感谢!
Alex
我很惊讶Rails的创造者没有考虑到这一点,如果有人能帮忙,那就太好了。
我们应该怎么做才能改变这个呢:
<%= link_to "My Title", :controller => "products" %>
自动执行此操作:
<%= link_to "My Title", :controller => "products", :title => "My Title" #basically a copy of the text %>
我认为这可以大大帮助SEO。
非常感谢!
Alex
这就是 Rails 3 的方式:
<%= link_to object_path, title: "Path Title" %>
更多阅读:https://www.searchenginejournal.com/how-to-use-link-title-attribute-correctly/
你的问题非常合理,我不知道为什么有人给你点了负分,但是,rails的创建者确实考虑过这个问题。实际上,你可以用一种非常简单的方法来解决,而不需要使用自定义方法使它变得复杂:
<%= link_to "Link", { :action => "show" }, { :title => "Title" } %>
试试这样的东西
def link_to_with_autotitle(title, args = {})
link_to_without_autotitle(title, args.merge(:title => title))
end
alias_method_chain :link_to, :autotitle
我还没有测试过这段代码,也不记得确切的link_to规范,但我想你应该明白我的意思。