Search code examples
ruby-on-railslink-to

external link not working as field display value in Rails


I'm looking to have the display value working as an external link.

<a href="<%= contact.website %>"><%= contact.website %></a>

This is the current code, but its result does not bring focus out of the app.

I attempted a link_to. This works but does not display the actual field value but the word "website" as the display.

<%= link_to("website", contact.website) %>

How can <%= contact.website %> have current syntax within the "" quotes?


Solution

  • I think you are looking for:

    <%= link_to(contact.website, contact.website, target: '_blank') %>
    

    As you see, you can just provide contact.website as link text. Also, target: '_blank' will open the web in a new browser tab.