I am a beginner and I have a really basic question confusing me ie
for example we have before_action and after_action in rails 4 but we call them filters, why? I read somewhere that filters are for controllers and actions are for models, is that it?
OK, so this is how I see things:
1: An action is a method of a controller to which you can route to. For example, your user creation page might be routed to UsersController#new
- new is the action in this route.
2: Filters run in respect to controller actions - before, after or around them. These methods can halt the action processing by redirecting or set up common data to every action in the controller. For example:
before_action :require_logged_out, only: [:new]
def require_logged_out
redirect_to user_path(current_user) if logged_in?
end
So here, require_logged_out
is a filter which runs before the new action of the users controller.
3: Lastly I'll mention that in Rails 3.x *_action
filters have been defined via *_filter
. for example, you'd set a before_filter
and not a before_action
.
I think that should wrap things up for you regarding actions and filters in rails.
Actions are the rails 4.0 name for filters. They do the exact same thing but before_action is a clearer name for the behavior.
Here is where the name was changed in code. https://github.com/rails/rails/commit/9d62e04838f01f5589fa50b0baa480d60c815e2c
Here is a link to another discussion on the matter. Rails 4: before_filter vs. before_action
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.