LoginSignup
25
22

More than 5 years have passed since last update.

Rails4系ですべてのhelperがviewから読めるのを禁止する設定

Last updated at Posted at 2014-11-07

rails g系のコマンドでコントローラーと一緒に生成されて、さもネームスペース切られてまっせ的な雰囲気を醸し出しているhelperさんですが、どうやら一緒くたんにロードされているらしく、どのviewからも余裕でメソッドコール出来る始末。

1コントローラーに全部インクルードされるんですか!気持ち悪いですね!ということで色々調べた結果、詳細はちゃんとrelase noteに記述してあったというオチ。

3_1_release_notes.md

* Added `config.action_controller.include_all_helpers`. By default `helper :all` is done in `ActionController::Base`, which includes all the helpers by default. Setting `include_all_helpers` to `false` will result in including only application_helper and the helper corresponding to controller (like foo_helper for foo_controller).

なので、

config.action_controller.include_all_helpers = false

を設定すればおk。

ううーむ。includeするしないの処理が重たいとか...?デフォルトでオンになってる理由はわかんなかったです。

追記

設定を切り替えた際に、helperがちゃんと存在するかどうかは、RSpecのControllerの設定で拾えそう

describe HogeController do
  # この設定をすると実際にviewを描画する
  render_views
  ...
end
25
22
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
25
22