Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | Rails4 | before_filterからbefore_actionへの変更

概要

Rails4 before_filterからbefore_actionへの変更

内容

Rails4からbefore_filterなどの命名がbefore_actionへの変更になりました。

サンプル

controller/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authorize # => new_style
  # before_filter :authorize => old_style

  def authorize
    return redirect_to :controller => 'signin', :action => 'index' unless session[:user_id]
  end
  
  # 他の処理
end