Format and validate a URL attribute in Active Record. This is an example gem created for RailsCasts episode #301.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install urrl_formatter
format_url in an ActiveRecord class and pass the name of the attribute you wish to format into a URL and validate.
class Comment < ActiveRecord::Base format_url :website end
This will automatically add "http://" to the beginning of the
website attribute upon saving if no protocol is present. It will also do validation to ensure it looks like a URL.
Questions or problems? Please post them on the issue tracker. You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running
This gem is created by Ryan Bates and is under the MIT License.