NoMethodError when trying to invoke helper method from Rails controller

前端 未结 14 1196
滥情空心
滥情空心 2020-11-30 20:05

I\'m getting a NoMethodError when trying to access a method defined in one of my helper modules from one of my controller classes. My Rails application uses the

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 20:52

    To use the helper methods already included in the template engine:

    • Rails 2: use the @template variable.
    • Rails 3: has the nice controller method view_context

    Example usage of calling 'number_to_currency' in a controller method:

    # rails 3 sample
    def controller_action
      @price = view_context.number_to_currency( 42.0 ) 
    end
    
    # rails 2 sample
    def controller_action
      @price = @template.number_to_currency( 42.0 ) 
    end
    

提交回复
热议问题