LoginSignup
12
13

More than 5 years have passed since last update.

アクセスしているページのコントローラに関する名称を取得

Posted at

controllers/application_controller.rbviews/layouts/application.html.erbなんかで、画面毎に処理を分けたい場合に有効。
controller.controller_nameは純粋にコントローラ名しか取れないので、
名前空間(モジュール?)を使っている場合には同名のコントローラ名が存在する可能性がある。
なので、判定にはモジュール名も併せてみるか、コントローラクラス名を使うと確実かも。

コントローラが入っているモジュール名
controller.class.parent.name

コントローラクラス名
controller.class.to_s

コントローラ名
controller.controller_name

アクセスしているアクション名
controller.action_name

12
13
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
12
13