How to call ApplicationController methods from ApplicationHelper

后端 未结 2 2008
野趣味
野趣味 2021-02-01 13:11

I want to provide csv links in a view and I placed the csv generating code in ApplicationHelper. However I\'m getting this error:

undefined method `         


        
2条回答
  •  一生所求
    2021-02-01 13:49

    Use helper_method.

    By default, methods in ApplicationController are only accessible inside the Controllers.

    Add a method to the ApplicationController and expose it as a helper method with helper_method:

    class ApplicationController < ActionController::Base
    
      helper_method :foo
    
      def foo
        "bar"
      end
    
    end
    

    Now the foo method is accessible to both Controllers and Views.

提交回复
热议问题