Calling a controller action from script/console or runner

Is there a way to call or invoke a controller method from script/console
or script/runner? The reason is that I’d like to pre-cache some of the
actions on my controller.

The pseudo code below is what I’d like to be able to do:

class MyController < ApplicationController

caches_action :test

def test
puts ‘Hi there’
end

end

script/console:

MyController.test
=> ‘Hi there’

Thanks.

Hi Mark, it seems that you’re trying to invoke an instance using class
method syntax. Thus, you’ll need to do the following in
script/console:

  1. controller = MyController.new

  2. controller.test

Good luck,

-Conrad

I see, cool. Thanks Conrad!

Do you think that’s the best way to pre-cache some of the controller’s
actions?

Thanks.

Conrad T. wrote:

Hi Mark, it seems that you’re trying to invoke an instance using class
method syntax. Thus, you’ll need to do the following in
script/console:

  1. controller = MyController.new

  2. controller.test

Good luck,

-Conrad

Hello Mark, Mark and Mark.

How would you pass parameters to that call, but I mean parameters that the body of test will refer to as params?

@jgomo3 That is a very old post (from 15 years ago), so it is very doubtful the poster would respond. But additionally, it seems like a fake trolling post. The content seems to go nowhere and it does not seem to provide any true value. I think the person was acting the dialog just to take up time and space like trolls do (and they sometimes do it in a way that makes it difficult to prove it is trolling by making the dialog seem a bit realistic). I would stay away from these posts. It’s better not to give them any attention or respond to them in the first place. But, keep your interesting posts and questions coming. I appreciate that there are real people (non-trolls and non-spammers) posting here every once in a while. Cheers!

2 Likes