SEO/Rails - How to add the title tag to every "link_to"
I'm surprised Rails creator didn't开发者_C百科 think about that, if someone can help, would be great.
How can we do to change this:
<%= link_to "My Title", :controller => "products" %>
to this automatically:
<%= link_to "My Title", :controller => "products", :title => "My Title" #basically a copy of the text %>
I think it could help SEO a lot.
Thanks a lot!
Alex
This is the rails 3 way:
<%= link_to object_path, title: "Path Title" %>
Further reading: https://www.searchenginejournal.com/how-to-use-link-title-attribute-correctly/
Your question is valid and I don't know why you are down-voted, but, the creator of rails DID actually think about this. Actually, you can do it in a very simple manner instead of complicating using a custom method:
<%= link_to "Link", { :action => "show" }, { :title => "Title" } %>
You can in fact add any parameter you like, not just the title.
Hope this helps!
Try something like that
def link_to_with_autotitle(title, args = {})
link_to_without_autotitle(title, args.merge(:title => title))
end
alias_method_chain :link_to, :autotitle
Haven't tested the code and do not remember the exact link_to spec but I think you get the idea
精彩评论