Question

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

Was it helpful?

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

OTHER TIPS

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top