LoginSignup
5
4

More than 5 years have passed since last update.

Viewの中でメソッドがどこで定義されているか調べる方法

Last updated at Posted at 2015-04-23

erbファイルの中ではnotice/alertに限り、flashを省略して書けるらしい。

app/views/layouts/application.html.erb
....
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

これは下記と同じ意味

app/views/layouts/application.html.erb
....
<p class="notice"><%= flash[:notice] %></p>
<p class="alert"><%= flash[:alert] %></p>

なぜそういうふうになっているのか気になったので、どこで定義されているのか知りたかったのだけど、ドキュメントを探してもよくわからなかった。

まず試してみたのがこれ。

app/views/layouts/application.html.erb
....
self.method(:notice).source_location

"actionpack-4.2.0/lib/abstract_controller/helpers.rb", 66

AbstractControllerのhelper_methodが呼ばれていることがわかる。
https://github.com/rails/rails/blob/master/actionpack/lib/abstract_controller/helpers.rb

これではどこでhelper_methodが呼ばれたかわからないので、こっちを実行してみる。

app/views/layouts/application.html.erb
....
ApplicationController.instance_method(:notice).source_location

"actionpack-4.2.0/lib/action_controller/metal/flash.rb", 35

これが目的のソースだということがわかる。add_flash_typesというクラスメソッドが定義されていて、:alert,:noticeに対して実行されている。

flash.rb
    included do
      class_attribute :_flash_types, instance_accessor: false
      self._flash_types = []

      delegate :flash, to: :request
      add_flash_types(:alert, :notice)
    end

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4