Archive for September 2009

Sep302009

Active Record Validator (DRY Approach)

Comments Off

With the release of Rails 2.3.4, It includes validators independent from models. Rails is going much more DRY.

For this one just have to define a class that inherits ActiveRecord::Validator and there you go

Let’s see an example:

1
2
3
4
5
6
class EmailValidator < ActiveRecord::Validator
	def validate
		record.errors[:email] << “is not valid” unless
		record.email =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
	end
end

Validate method override validate method of parent class. Now you can include this to any of your model.

Let’s see HOW?

1
2
3
class bussiness < ActiveRecord::Base
	validates_with: EmailValidator #That’s all
end
Sep162009

New Ways of rendering partials

Comments Off

I was working on project when I bumped into a new feature, rather an old one; I don’t care although, that we can render partial without writing partial or locals.

Check it out how?

before

1
    render :partial => 'students/notice_board', :locals => { :note =>@ note, :show_title => true}

this can be rewritten as:

1
    render 'students/notice_board', :note => @note, :show_title => true

and all those thing will be working as is as before.

Pamper your hands, try this

Three cheers.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes