1

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?

fguillen
  • 36,125
  • 23
  • 149
  • 210

2 Answers2

3

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>
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
1

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.

ZedTuX
  • 2,859
  • 3
  • 28
  • 58