URL validation

So I learned the hard way about URL validation when submitting a URL as a text string.

I am doing round 2 of building the “Reddit on Rails” app and submitted a URL in the format of “google.com” instead of “http://www.google.com”. When I would type something like <%= link_to @link.title, @link.url %> it would return a link in the format of “http://mydomain/links/google.com” instead of what I wanted which was “http://www.google.com”.

I thought something might be wrong with my routes.rb file, or links controller. Turns out I created the problem by not submitting the URL with the “http://” at the beginning of the string.

Did some Google searching and came up with a nice thread on how to validate URLs in rails.

Leave a Reply