# How to handle absolute URL that user inputs into a table?

**URL:** <https://www.ruby-forum.com/t/how-to-handle-absolute-url-that-user-inputs-into-a-table/174391>\
**Category:** Rails\
**Created:** [August 26, 2009, 11:15am UTC](https://www.ruby-forum.com/t/how-to-handle-absolute-url-that-user-inputs-into-a-table/174391 "2009-08-26T11:15:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnhHung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@AnhHung](https://www.ruby-forum.com/u/AnhHung)\
**Post date:** [August 26, 2009, 11:15am UTC](https://www.ruby-forum.com/t/how-to-handle-absolute-url-that-user-inputs-into-a-table/174391/1 "2009-08-26T11:15:44Z")

</div>

Hi,

I have a User table that has a Website column.

In the form I would have the user type in the URL to their website.

If they type the full-blown address “[http://www.example.com](http://www.example.com)” then  
“link\_to @user.website, @user.website” will work out of the box.

However, if they type in “[www.example.com](http://www.example.com)” then the above code would  
display something like: localhost:3000/controller/action… something  
like that.

How do I make it that no matter what the user type it, link\_to would  
take them to an external site?

Sorry if this sounds like a too basic question.

Thanks.

---

<div class="post-metadata">

**Author:** ![AnhHung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@AnhHung](https://www.ruby-forum.com/u/AnhHung)\
**Post date:** [August 26, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/how-to-handle-absolute-url-that-user-inputs-into-a-table/174391/2 "2009-08-26T12:06:05Z")

</div>

Depends on the protocol for the websites. It is difficult to parse  
all possible protocols. Assuming you are only talking about http, you  
can do something like

regex = /^(https?://)/  
@user.website = “http://” + @user.website unless regex.match(website)

---

<div class="post-metadata">

**Author:** ![AnhHung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@AnhHung](https://www.ruby-forum.com/u/AnhHung)\
**Post date:** [August 26, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/how-to-handle-absolute-url-that-user-inputs-into-a-table/174391/3 "2009-08-26T12:12:31Z")

</div>

2009/8/26 AnhHung [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> However, if they type in “[www.example.com](http://www.example.com)” then the above code would  
> display something like: localhost:3000/controller/action… something  
> like that.
> 
> How do I make it that no matter what the user type it, link\_to would  
> take them to an external site?
> 
> Sorry if this sounds like a too basic question.

You could check what the user enters and add the http:// if they have  
not provided it, either on the way into the database or out of it.

Colin

---

<div class="post-metadata">

**Author:** ![AnhHung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@AnhHung](https://www.ruby-forum.com/u/AnhHung)\
**Post date:** [August 26, 2009, 12:21pm UTC](https://www.ruby-forum.com/t/how-to-handle-absolute-url-that-user-inputs-into-a-table/174391/4 "2009-08-26T12:21:44Z")

</div>

> How do I make it that no matter what the user type it, link\_to would  
> take them to an external site?
> 
> Sorry if this sounds like a too basic question.
> 
> Thanks.

try this  
this may work

link\_to @user.website,“http://#{@user.website}”, :popup =\>“true”

---

<div class="post-metadata">

**Author:** ![AnhHung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@AnhHung](https://www.ruby-forum.com/u/AnhHung)\
**Post date:** [August 26, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/how-to-handle-absolute-url-that-user-inputs-into-a-table/174391/5 "2009-08-26T12:58:30Z")

</div>

> How do I make it that no matter what the user type it, link\_to would  
> take them to an external site?
> 
> Sorry if this sounds like a too basic question.
> 
> Thanks.

do this no matter what URL user enters it will take him to correct page

```
    regex = /https?:\/\/(.*)/.match(@user.website)
    if regex==nil
       link_to @user.website,"http://#{@user.website}", :popup 

```

=\>“true”  
else  
link\_to @user.website,"#{@user.website}", :popup =\>“true”
