Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a controller concern method from the console?

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?

like image 310
koosa Avatar asked Feb 12 '23 01:02

koosa


2 Answers

You can do this:

controller = ActionController::Base::ApplicationController.new
controller.extend(MyConcern)

controller.concern_method "hello world"
like image 165
fivedigit Avatar answered Feb 14 '23 13:02

fivedigit


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
like image 23
timnilson Avatar answered Feb 14 '23 15:02

timnilson