Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Validates and adds protocol to a url
tree: 66fa41b3c8
Failed to load latest commit information.
lib email validation added and url validation improved
test email validation added and url validation improved
.gitignore new files added to repository
README.rdoc url will now be converted after validations, testcases added
Rakefile first commit
init.rb first commit



This acts_as extension adds the protocol (eg 'http://') to a url database column if missing. It also validates the url against a regular expression.


A common usecase would be in blog comments where you want the user to be able to specify his/her website:

class Comment < ActiveRecord::Base
  acts_as_url :website

>> comment =
=> #<Comment:0x245fed8 ... >

The protocol is added just before the comment is saved to the database (as a before_save callback).

>> = ''
=> ""

=> true

=> ""

In this case protocol is already given and is therefore not added, however a trailing slash is appended.

>> = ''
=> ""

=> true

=> ""

The url will be validated before it is saved to the database.

>> = 'not a url'
=> "not a url"

>> comment.invalid?
=> true


By default allowed protocols are http and https, but you can set it specificly for an attribute as a hash value (with the attribute as the key):

acts_as_url :website, :repository => 'git'

If you want to allow several protocols, you can use an array:

acts_as_url :website => ['https', 'http']

Note that the first value in the array will always be the one prepended to the url if a protocol is omitted by the user.

>> = ''
=> ""

=> ""

Copyright © 2009 Molte Emil Strange Andersen, released under the MIT license

Something went wrong with that request. Please try again.