Rails 4: before_filter vs. before_action
Asked Answered
V

6

360

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?

Vomit answered 13/5, 2013 at 10:26 Comment(0)
V
546

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

Vomit answered 13/5, 2013 at 10:27 Comment(11)
On the one hand deprecating has sense but on the other there is a good practice in rails and in ruby to have several aliases for one method so you can use it in different contexts without loss of meaning.Vomit
In 4.2 They are not deprecating it, but removing it from the docs since it is discouraged. edgeguides.rubyonrails.org/…Bahadur
If it's discouraged, then why wouldn't you deprecate it? Silly.Varioloid
@GrantBirchmeier, it's "discouraged in favor of the *_action family of methods", and will be deprecated in the future per @jryancanty's link above. Even Rails has to occasionally bow to not breaking all of the APIs all of the time.Geophilous
@JohnWhitley - deprecation doesn't break the API. That's the whole point of deprecation.Varioloid
@GrantBirchmeier yes, you can technically ship with deprecation warnings, but the pain level is high since deprecation invariably spams the heck out of your test suite runs. For any well-disciplined project that keeps clean test runs, this becomes de facto breakage. Therefore, turning on deprecation warnings where there isn't a priority to remove the API in the immediately following release causes an unnecessary fire-drill among the Rails userbase.Geophilous
@JohnWhitley - The spamming is for a reason. Any well-disciplined project should address deprecation warnings by not using deprecated functions.Varioloid
It's deprecated. From wikipedia: "Deprecation is an attribute applied to a computer software feature, characteristic, or practice to indicate that it should be avoided". So essentially they deprecated it but have yet to mark it as such. Clear communication of these things is overrated.Thurmanthurmann
It's there: "If your application currently depends on these methods, you should use the replacement *_action methods instead. These methods will be deprecated in the future and will eventually be removed from Rails." edgeguides.rubyonrails.org/…Cowberry
They are officially deprecated in Rails 5 and their use will generate warnings.Lionize
The <callback>_filters are deprecated in Rails 5.0 and will be removed in 5.1: github.com/rails/rails/blob/v5.0.0.beta2/actionpack/lib/…Coelom
G
73

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.

Ginger answered 6/5, 2014 at 12:59 Comment(0)
I
33

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

Interstratify answered 13/5, 2013 at 10:33 Comment(0)
N
1

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.

Northing answered 30/4, 2018 at 10:52 Comment(0)
M
0

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...

Minardi answered 13/6, 2021 at 12:39 Comment(0)
P
-4

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

Pericline answered 6/4, 2017 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.