auto url formatting?


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, format_url( %>

And it would turn:

"" # => ""

but also:

"" # => ""


"" # => ""

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



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.


Thanks for the pointer! URI.parse has gotten me halfway there. The
problem is that if I pass in "" 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.


url = ""
uri = URI.parse(url)
full_url = uri.instance_of?(URI::Generic) ? "http://#{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.


URI parse won’t recognise that 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, how is URI.parse supposed to know you mean HTTP and not FTP or RSync?


I hear what you are saying and agree. But for my purposes, in a
Contacts database, if someone enters their company website as
"" or even "" I want to ASSUME "http". If
their company website is an FTP drop, then they'd have to put in
"" 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.


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

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

%w{}.each do |website|
  uri = URI.parse(website)
  uri.scheme ||= 'http'
  p uri.to_s

#=> ‘’, ‘