Thursday, May 14, 2015

Renamed Callbacks in Rails 4

Action callbacks in controllers have been renamed from *_filter to *_action.
For example:

class UsersController < ApplicationController
  before_action :set_user, :except => [:index, :new, :create}
  before_action :require_the_president, :only => [:fire_the_missiles]
 
  private
 
  def set_user
    @user = somehow_find_and_set_the_user(params[:id])
  end
 
  def require_the_president
    @user.is_the_president?
  end
end 

The old *_filter callbacks still work and are not deprecated; so, you can still use them if you wish.

No comments:

Post a Comment