# Auto url formatting?

**URL:** <https://www.ruby-forum.com/t/auto-url-formatting/137862>\
**Category:** Rails\
**Created:** [May 17, 2008, 12:37am UTC](https://www.ruby-forum.com/t/auto-url-formatting/137862 "2008-05-17T00:37:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [May 17, 2008, 12:37am UTC](https://www.ruby-forum.com/t/auto-url-formatting/137862/1 "2008-05-17T00:37:33Z")

</div>

Hello!

I’m having a bear finding this, so I thought I’d just ask…

If I want to capture “website” as a user field (i.e. in a contacts  
database)… is there an easy, automatic way to properly format it for  
a link\_to tag regardless of whether they include the “http” or not?  
I.e. some helper or something that someone has that I could do  
something like:

\<%= link\_to @user.website, format\_url(@user.website) %\>

And it would turn:

“[www.google.com](http://www.google.com)” # =\> “[http://www.google.com/](http://www.google.com/)”

but also:

“[http://www.google.com](http://www.google.com)” # =\> “[http://www.google.com/](http://www.google.com/)”

and:

“ftp://ftp.google.com” # =\> “ftp://ftp.google.com/”

I imagine there are some code snippits for doing this… I just  
haven’t found one yet.

Thanks!

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [May 17, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/auto-url-formatting/137862/2 "2008-05-17T21:32:13Z")

</div>

Have you looked at using URI.parse? I think you should be able to use  
the returned object to determine what parts were provided. You could  
either store it as provided and parse/rebuild on the way out or  
“clean” it as it’s stored.

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [May 18, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/auto-url-formatting/137862/3 "2008-05-18T16:30:42Z")

</div>

Andy,

Thanks for the pointer! URI.parse has gotten me halfway there. The  
problem is that if I pass in “[www.foobar.com](http://www.foobar.com)” I get a URI::Generic  
object instead of a URI::HTTP (or other protocol) object.

So, I can make this work, I think, but I have to check if the return  
of URI.parse is URI::Generic and if so, then explicitly prepend the  
“http://” for link\_to’s.

I.e.

```auto
url = "www.foobar.com"
uri = URI.parse(url)
full_url = uri.instance_of?(URI::Generic) ? "http://#{uri.to_s}" :
uri.to_s
```

Not ideal but it works. I suppose I’d rather have something like:

uri = URI.parse(url, :default =\> “http”)

and that would return a URI::HTTP is no protocol is defined, instead  
of a URI::Generic. Ah well. One extra if/then chunk isn’t too bad.

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [May 19, 2008, 3:51am UTC](https://www.ruby-forum.com/t/auto-url-formatting/137862/4 "2008-05-19T03:51:53Z")

</div>

URI parse won’t recognise that [www.domain.com](http://www.domain.com) is actually a http url as  
www  
is just some subdomain name that a whole ton of people love getting to  
point  
to their main site. It’s archaic, and I hate it. Think of it if it was  
[radar.domain.com](http://radar.domain.com), how is URI.parse supposed to know you mean HTTP and  
not  
FTP or RSync?

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [May 19, 2008, 4:14am UTC](https://www.ruby-forum.com/t/auto-url-formatting/137862/5 "2008-05-19T04:14:07Z")

</div>

You could always get it to do some code when it detects a Generic and  
automatically add a http://, on a before\_save perhaps.

before\_save do  
case URI.parse(site).class  
when URI::Generic  
site = “http://” + site  
end  
end

That way it will automatically stick http:// before URLs that are  
“vague”  
and not urls with any other prefix.

On Mon, May 19, 2008 at 11:39 AM, Danimal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> it can’t match it to one of the specific protocols. So, in my case,
> 
> > URI parse won’t recognise thatwww.domain.comis actually a http url as www  
> > is just some subdomain name that a whole ton of people love getting to  
> > point  
> > to their main site. It’s archaic, and I hate it. Think of it if it was  
> > [radar.domain.com](http://radar.domain.com), how is URI.parse supposed to know you mean HTTP and  
> > not  
> > FTP or RSync?

–  
Appreciated my help?  
Reccommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [May 19, 2008, 1:35pm UTC](https://www.ruby-forum.com/t/auto-url-formatting/137862/6 "2008-05-19T13:35:07Z")

</div>

%w{[www.google.com](http://www.google.com) [http://www.google.com](http://www.google.com)}.each do |website|  
uri = URI.parse(website)  
uri.scheme ||= ‘http’  
p uri.to\_s  
end

#=\> ‘http:www.google.com’, ‘[http://www.google.com](http://www.google.com)’

On May 18, 10:13 pm, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [May 19, 2008, 4:10am UTC](https://www.ruby-forum.com/t/auto-url-formatting/137862/7 "2008-05-19T04:10:36Z")

</div>

Ryan,

I hear what you are saying and agree. But for my purposes, in a  
Contacts database, if someone enters their company website as  
“[www.google.com](http://www.google.com)” or even “[google.com](http://google.com)” I want to ASSUME “http”. If  
their company website is an FTP drop, then they’d have to put in  
“ftp://companyco.com” or whatever.

The beauty of URI.parse is that it’ll return a URI::Generic class if  
it can’t match it to one of the specific protocols. So, in my case,  
I’ll assume “http” and move forward. If it turns out to be wrong, they  
(the users) can always explicitly add the protocol.

I suppose I could have done the same thing with a regexp, but given  
that I’m dealing with URIs, I like the URI module/class better.

-Danimal

On May 18, 7:50 pm, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
