“undefined method” when calling helper method from controller in Rails

后端 未结 11 2104
傲寒
傲寒 2020-12-12 22:36

Does anyone know why I get

undefined method `my_method\' for #

when I call my_method(\"string\") from withi

11条回答
  •  没有蜡笔的小新
    2020-12-12 22:46

    You cannot call helpers from controllers. Your best bet is to create the method in ApplicationController if it needs to be used in multiple controllers.

    EDIT: to be clear, I think a lot of the confusion (correct me if I'm wrong) stems from the helper :all call. helper :all really just includes all of your helpers for use under any controller on the view side. In much earlier versions of Rails, the namespacing of the helpers determined which controllers' views could use the helpers.

    I hope this helps.

提交回复
热议问题