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:
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 "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.
URI parse won’t recognise that 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, 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
"www.google.com" or even "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.