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" then "link_to @user.website, @user.website" will work out of the box. However, if they type in "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.
Colin Law
2009-Aug-26 09:59 UTC
Re: How to handle absolute URL that user inputs into a table?
2009/8/26 AnhHung <anhhung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > 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" then > "link_to @user.website, @user.website" will work out of the box. > > However, if they type in "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
Mukund
2009-Aug-26 10:04 UTC
Re: How to handle absolute URL that user inputs into a table?
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) On Aug 26, 2:14 pm, AnhHung <anhh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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" then > "link_to @user.website, @user.website" will work out of the box. > > However, if they type in "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.
Nik Cool
2009-Aug-26 10:21 UTC
Re: How to handle absolute URL that user inputs into a table?
> > 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" -- Posted via http://www.ruby-forum.com/.
Nik Cool
2009-Aug-26 10:58 UTC
Re: How to handle absolute URL that user inputs into a table?
> > 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" -- Posted via http://www.ruby-forum.com/.