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/" but also: "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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On May 16, 6:36 pm, Danimal <fightonfightw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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/" > > but also: > > "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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andy, 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. I.e. [code] url = "www.foobar.com" uri = URI.parse(url) full_url = uri.instance_of?(URI::Generic) ? "http://#{uri.to_s}" : uri.to_s [/code] 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 On May 17, 1:31 pm, AndyV <AndyVana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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" 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. -Danimal On May 18, 7:50 pm, "Ryan Bigg (Radar)" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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, how is URI.parse supposed to know you mean HTTP and not > FTP or RSync?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <fightonfightwell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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" 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. > > -Danimal > > On May 18, 7:50 pm, "Ryan Bigg (Radar)" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > 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, 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
%w{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'' On May 18, 10:13 pm, "Ryan Bigg (Radar)" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 <fightonfightw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > > > 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" 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. > > > -Danimal > > > On May 18, 7:50 pm, "Ryan Bigg (Radar)" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > URI parse won''t recognise thatwww.domain.comisactually 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? > > -- > Appreciated my help? > Reccommend me on Working With Railshttp://workingwithrails.com/person/11030-ryan-bigg--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---