Rails 4: before_filter vs. before_action

Ruby on-RailsRubyRuby on-Rails-4Crud

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

It is just a name change. before_action is more specific, because it gets executed before an action.

Solution 4 - Ruby on-Rails

before_filter/before_action: means anything to be executed before any action executes.

Both are same. they are just alias for each other as their behavior is same.

Solution 5 - Ruby on-Rails

use only before_action with rspec-rails, capybara as before_filter will misbehave to give surprises during testing

class TodosController < ApplicationController
  before_filter :authenticate

  def index
    @todos = Todo.all
  end
  ## Rest of the code follows
end

before_filter

feature 'User creates todo' do
  scenario 'successfully' do
    sign_in
    click_on 'Add Todo'
    fill_in 'Title', with: "Buy Milk"
    click_on 'Submit'

    expect(page).to have_css '.todos li', text: "Buy Milk"
  end
end

the expected failure is

NoMethodError:
       undefined method `authenticate' for #<TodosController:0x0000558b68573f48>

but before_filter gives...

ActionView::Template::Error:
       undefined method `each' for nil:NilClass

That is, somehow the hook runs without error and but the controller goes to view with a @todos uninitialized Better save time, use non deprecated codes...

Solution 6 - Ruby on-Rails

To figure out what is the difference between before_action and before_filter, we should understand the difference between action and filter.

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.

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.

> Rails 4 –> _action > > Rails 3 –> _filter

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfreemanoidView Question on Stackoverflow
Solution 1 - Ruby on-RailsfreemanoidView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAwaisView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMatthiasView Answer on Stackoverflow
Solution 4 - Ruby on-RailsPankaj DhoteView Answer on Stackoverflow
Solution 5 - Ruby on-RailsiarunpaulView Answer on Stackoverflow
Solution 6 - Ruby on-RailsyusefuView Answer on Stackoverflow