10

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

JJD
  • 50,076
  • 60
  • 203
  • 339
Alextoul
  • 839
  • 3
  • 9
  • 19
  • 4
    That wouldn't help SEO at all — it would be completely redundant. It would add ugly tooltips all over the place and repeat lots of information to screen reader users. Use a title attribute when you need to provide additional information that you either can't include in the link text or isn't important enough to do so. – Quentin Sep 23 '10 at 20:50
  • I'm surprised you want to do this, isn't the link text enough...? – Matti Virkkunen Sep 23 '10 at 20:51
  • David's answer make sense but I had the experience before. It's better to repeat the text than to leave the title tag empty. And I don't have time to specify "additional information" to all of my links. Thanks to both of you for the interest. – Alextoul Sep 23 '10 at 21:15
  • No title tag? What's that got to do with title attributes...? What does "I had the experience before" mean? o_O – Matti Virkkunen Sep 23 '10 at 21:16

3 Answers3

15

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/

Abram
  • 39,950
  • 26
  • 134
  • 184
9

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!

dsignr
  • 2,295
  • 2
  • 35
  • 45
-5

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

cryo28
  • 1,127
  • 6
  • 9
  • 1
    Wouldn't args.merge(:title => title) actually overwrite the title in args, in case one was specified? { :title => title }.merge(args) should be right. – Manuel Meurer Nov 17 '10 at 22:22