Rails, link_to helper with an URL as a param

460 Views Asked by At

I want to generate the next html link:

<a href="http://url.com">http://url.com</a>

To reproduce it using the link_to helper I have to write:

<%= link_to "http://url.com", "http://url.com" %>

What doesn't look DRY at all, I was expecting this to work:

<%= link_to "http://url.com" %>

But the above code generate a link targeting the actual request.url, not the one I'm sending in the param.

Am I missing something?

2

There are 2 best solutions below

0
On BEST ANSWER

You're not missing anything --- the normal case is for the URL and the text that shows to the user to be different.

If you'd like, you could create a helper like

def link_to_href(link, args={})
  link_to link, link, args
end

then, when you use it,

<%= link_to_href "http://url.com" %>

Will output

<a href="http://url.com">http://url.com</a>
0
On

If you take a look at the source code of link_to you will see that at line 248 the a tag label is build with name || url.

That's why you have this behaviour and there is noway to do it like you're expecting.