6

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?

Community
  • 1
  • 1
koosa
  • 2,966
  • 3
  • 31
  • 46

2 Answers2

6

You can do this:

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

controller.concern_method "hello world"
fivedigit
  • 18,464
  • 6
  • 54
  • 58
5

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
timnilson
  • 121
  • 1
  • 5