LoginSignup
40
51

More than 5 years have passed since last update.

viewから使えるhelperはデフォルトでController名と同じやつ

Last updated at Posted at 2015-01-07

controllerでは全部のhelperをincludeしているようですが、
viewではController名と同名のhelperしか読み込まないだそうな。

なので、

app/helpers/application_helper.rb
module ApplicationHelper
  def common_helper_method
  end
end
app/helpers/users_helper.rb
module UsersHelper
  def user_helper_method
  end
end

common_helper_methodは、ApplicationControllerを継承したどのControllerのViewからも使えるが、user_helper_methodUsersControllerのViewからしか使えない。

他のControllerのViewから使いたい場合は以下のようにする

app/controllers/others_controller.rb
class OthersController < ApplicationControlelr
  helper Users
end

Rails4系ですべてのhelperがviewから読めるのを禁止する設定 - Qiitaを見る限り、config.action_controller.include_all_helpers = trueであれば、viewから全Helper読めるような感じするんですけど、どうなんだろう。

40
51
1

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
40
51