If I have a controller concern, for example:
module MyConcern
def concern_method param
puts param.inspect
end
end
How can I test the method concern_method
from the console?
Using methods described here: How do I call controller/view methods from the console in Rails? I can access the application controller using:
controller = ActionController::Base::ApplicationController.new
... but then this throws an error:
controller.concern_method "hello world"
NoMethodError: undefined method `concern_method` for #<ApplicationController:0x000001091fbad0>
Is the concern not added automatically to the controller when it is instantiated from the console?
You can do this:
controller = ActionController::Base::ApplicationController.new
controller.extend(MyConcern)
controller.concern_method "hello world"
To access a concern in the Rails 6 console, first run include in the console
include MyConcern
Then run your method in the console
concern_method
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