Search code examples
ruby-on-railslink-tourl-helper

Rails, link_to helper with an URL as a param


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?


Solution

  • 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>