Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: before_filter vs. before_action

In rails >4.0.0 generators creates CRUD operations with before_action not before_filter. It seems to do the same thing. So what's the difference between these two?

like image 953
freemanoid Avatar asked May 13 '13 10:05

freemanoid


2 Answers

As we can see in ActionController::Base, before_action is just a new syntax for before_filter.

However the before_filter syntax is deprecated in Rails 5.0 and will be removed in Rails 5.1

like image 131
freemanoid Avatar answered Sep 19 '22 06:09

freemanoid


It is just syntax difference, in rails app there is CRUD, and seven actions basically by name index, new, create, show, update, edit, destroy.

Rails 4 make it developer friendly to change syntax before filter to before action.

before_action call method before the actions which we declare, like

before_action :set_event, only: [:show, :update, :destroy, :edit] 

set_event is a method which will call always before show, update, edit and destroy.

like image 36
Awais Avatar answered Sep 22 '22 06:09

Awais