Skip to content

Instantly share code, notes, and snippets.

@yucao24hours
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save yucao24hours/975838aafc97df8d73c6 to your computer and use it in GitHub Desktop.

Select an option

Save yucao24hours/975838aafc97df8d73c6 to your computer and use it in GitHub Desktop.
2014/10/26 Rails3 レシピブック読書会

範囲

p.49 025 コントローラ全体の共通のメソッドを追加する から

p.53 027 アクションの前または後にフィルタを適用する まで

helper_method

application_controller に helper_method 宣言をすると、そのメソッドはビューからも使えるようになる。

controller の名前空間

module の中で class を宣言することができる。宣言したクラスはモジュール名の名前空間が付与される。

module Momomo
  class FooBar
  end
end

# 以下のようにアクセスする
Momomo::FooBar

参考ページ

filter オブジェクト

複数のコントローラで同じフィルタを使うときは、フィルタオブジェクトを作ると良い。 適用したいフィルタの種類(コールバックされるタイミング)によって before, after, filter メソッドのいずれかを実装していなければならない。

before_filter と before_action の違い

よくどちらの名前も聞くが、何が違うのかと調べてみたところ、Rails4 からは before_filterbefore_action という名前に変わったとのこと。 同様に、*_filter 系のコールバックの名前はすべて *_action に変わった。 https://github.com/rails/rails/commit/9d62e04838f01f5589fa50b0baa480d60c815e2c

参考: 「壊れてねぇなら直すな」という発想はRailsにはないのかも - QA@IT公式ブログ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment