Aluxzi
Aluxzi

Reputation: 177

Create Link to External Website

I am trying to create an external link to each individual listing's assigned website address. Using the following code: (The listing website is saved as google.com)

<a href="<%= listing.website %>">External Link</a>

Takes me to:

localhost:3000/google.com

Is there any way to generate a link that would go to www.google.com instead of trying to find a route in my application.

Upvotes: 0

Views: 362

Answers (4)

Dheer
Dheer

Reputation: 793

You can create link like this:

<a href="http://www.google.com", target: "_blank">External Link</a>

Upvotes: 0

Agis
Agis

Reputation: 33626

Do it the Rails way:

<%= link_to 'External Link', "http://#{listing.website}" %>

Upvotes: 2

Simone Carletti
Simone Carletti

Reputation: 176392

The reason why it's bringing you to localhost:3000/google.com it's probably because the string you are passing to the href attribute is not a full qualified URL.

In fact, if in HTML you write

<a href="google.com">External Link</a>

The string will be appended to the current page path. You should make sure that the input you pass always contains the schema. If the input never contains that, then you can assume it's http://

<a href="http://<%= listing.website %>">External Link</a>

But this is not really a solution, just a workaround. You should definitely make sure that when you populate the website URL, you store a complete URL. In fact, some sites may require https.

In Rails you normally use the url_for and link_to helpers to generate an URL, but they will both cause the same issue unless you pass a full URL.

<%= link_to "External Link", "http://#{listing.website}" %>

Upvotes: 2

user3210045
user3210045

Reputation:

You need to put in the protocol.

<a href="http://www.google.com">Google</a>

Do you get it? =)

Upvotes: 0

Related Questions