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 helper
class method with the :all
symbol as shown below:
class ApplicationController < ActionController::Base helper :all . . end
My understanding is that this should make all of my controller classes automatically include all of the helper modules within the app/helpers directory, therefore mixing in all of the methods into the controllers. Is this correct?
If I explicitly include
the helper module within the controller then everything works correctly.
In Rails 5, by using the new instance level helpers method in the controller, we can access helper methods in controllers.
A Helper method is used to perform a particular repetitive task common across multiple classes. This keeps us from repeating the same piece of code in different classes again and again. And then in the view code, you call the helper method and pass it to the user as an argument.
A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.
To use the helper methods already included in the template engine:
@template
variable. 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With