SEO / Rails - How to add a title tag to each "link_to"

I am surprised that the Rails creator did not think about it, if someone could help, it would be great.

How can we do this:

<%= link_to "My Title", :controller => "products" %> 

to do this automatically:

 <%= link_to "My Title", :controller => "products", :title => "My Title" #basically a copy of the text %> 

I think this can help SEO a lot.

Thank you so much!

Alex

+6
ruby-on-rails seo link-to
source share
3 answers

Try something like this

 def link_to_with_autotitle(title, args = {}) link_to_without_autotitle(title, args.merge(:title => title)) end alias_method_chain :link_to, :autotitle 

I didnโ€™t check the code and donโ€™t remember the exact specification of link_to, but I think you understood the idea

-4
source share

These are 3 way rails:

 <%= link_to object_path, title: "Path Title" %> 

Further reading: https://www.searchenginejournal.com/how-to-use-link-title-attribute-correctly/

+11
source share

Your question is correct, and I donโ€™t know why you voted, but the rail creator really thought about it. Actually, you can do this very simply, instead of complicating the use of the custom method:

 <%= link_to "Link", { :action => "show" }, { :title => "Title" } %> 

In fact, you can add any parameter you want, not just the title.

Hope this helps!

+7
source share

All Articles