koosa
koosa

Reputation: 3040

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?

Upvotes: 6

Views: 3662

Answers (2)

timnilson
timnilson

Reputation: 121

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

Upvotes: 5

fivedigit
fivedigit

Reputation: 18682

You can do this:

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

controller.concern_method "hello world"

Upvotes: 7

Related Questions