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 ?
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'
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
You are storing URLs without the http:// so they are being interpreted as relative URLs. Try this: link_to @user.site, "http://#{@user.url}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With