Malicious enjoyment derived from observing someone else’s misfortune
 

Tag Archives: validation

Doing things in models on save/update

So, I wanted to do some sweet gsubbing before the save of projects in Rails. I found the API docs had a wealth of information about this, but I figured I’d post my method anyway: before_create :fix_amount before_update :fix_amount Now you just define them under private in the model: def fix_amount self.amount.gsub!(/^\$/, "") end This takes those pesky $’s right out, back to the back yard where they belong.

HTTP URL Validation Improved

I found the HTTP URL Validator for Rails very interesting, and well coded, yet it lacked some things such as URL format restrictions. I added some things, and I came up with a sweet solution. It checks the format of the given URL, the content type, and whether it was permanently moved. I might be adding to this in the future. #Check for content type: validates_http_url :url, :content_type => "text/html" #Do not check for content type, just make sure the [...]