Search code examples
ruby-on-railsseolink-to

SEO/Rails - How to add the title tag to every "link_to"


I'm surprised Rails creator didn't 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


Solution

  • 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