Bambinos RSS

New Bamboo

Bamboo Blog

Archive

Sep
25th
Thu
permalink ismasan

Normalize params

Sometimes we need to make sure there is a key in the params hash, even when it’s not defined in the routes.rb file.

Ie. params[:page]. Instead of validating it in separate actions, just make sure it’s there in a before_filter

before_filter :normalize_params

protected

def normalize_params
params[:page] ||= 1
# some more normalization if needed
end


Then you can be confident that it exists:

def	index
 @products = Product.paginate(:all, :page => params[:page] )
end


Of course the ideal solution is to have default parameters in the routes file, but not always possible.