سؤال

I have a rails application. I need to use a helper within a presenter class. I used require 'number_helper' and used a method 'test' from 'number_helper'. It is giving me an error that 'undefined method 'test''.

How do I require a helper file in a presenter class. If I use 'include NumberHelper' it works. what is the wrong with my usage of 'require'

هل كانت مفيدة؟

المحلول

In Rails, Helpers are modules which are directly accessible in immediate view but in order to access them in a controller you need use include directive to specify the module explicitly.

In your case, test is an instance method. If you wish to access it in a class then either you need to include the module as specified above.

BUT if you want to access it using require 'number_helper' then define test as a class method(def self.test) and access it in controller as NumberHelper::test.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top