Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (40 sloc) 1.95 KB

URL field

Sometimes you want to accept a URL field on one of your models. Take for example a Company model with a website field. You want the URL to always have “http://” but sometimes users enter http://, sometimes they dont.

Your app shouldn’t care. Enter the URL field plugin.

This simple plugin allows you to enter either “” or just “” and in either case it will store “”


class Company < ActiveRecord::Base
  url_field :website

@company = = "" # => ""

Https? That works too (but one way only) = "" # => ""


Multiple fields

If you have multiple url_fields in a single model, just pass them as arguments to the url_field method, eg:

class Company < ActiveRecord::Base
  url_field :website, :support_website, :more_info_website

Access the correctly formed URL at any time

If you want access to the correctly formatted URL at any time (for example if you’re passing it to URI.parse, before you save your model), you can prefix your URL field method name with “cleaned_” eg. “cleaned_website” if your field name was website:

class Company < ActiveRecord::Base
  url_field :website

@company = = ""
@company.cleaned_website # => ""



./script plugin install git://

About me

I’m Paul Campbell. I’m an avid Ruby on Rails web developer. Follow my ramblings at

Follow me on Twitter

Copyright © 2009 Paul Campbell, released under the MIT license