Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RAILS link_to external site, url is attribute of user table, like: @users.website

I'm working on a website that allows users to create an account. One of the attributes when creating a user is a users personal website. When I try to use the users website like this:

<%= link_to @user.site, @user.url %>

The url that gets generated is: http://0.0.0.0:3000/www.userswebsite.com

I think this is because of the @user part of the link_to... but how can I get this to link to www.userwebsite.com ?

like image 524
thedeepfield Avatar asked Feb 16 '11 03:02

thedeepfield


3 Answers

You can prepend url with protocol if it's absent:

module UrlHelper   def url_with_protocol(url)     /^http/i.match(url) ? url : "http://#{url}"   end end 

And then:

link_to @user.site, url_with_protocol(@user.url), :target => '_blank' 
like image 88
Voldy Avatar answered Sep 28 '22 04:09

Voldy


Looks like you need to stick the protocol on your link. E.g. you have www.userswebsite.com in your database, it should be http://www.userswebsite.com

like image 30
cam Avatar answered Sep 28 '22 06:09

cam


You are storing URLs without the http:// so they are being interpreted as relative URLs. Try this: link_to @user.site, "http://#{@user.url}"

like image 23
Anshul Garg Avatar answered Sep 28 '22 05:09

Anshul Garg