Tuesday, July 20, 2010

Passing parameters to before_filter methods in Rails

If you have methods in your controller class that requires parameters you can still user them as filters.
Example:
def require_role(role)
#do some stuff with 'role' parameter
end
We can use it like this:
before_filter do |controller_instance|
controller_instance.require_role :admin
end
In fact, you can still use :only and :except if you need it, example:
before_filter :only => :index do |controller_instance|
controller_instance.require_role :admin
end
Have a nice day.

No comments: