Sid Wilksin
Sid Wilksin

Reputation: 11

What can I use at the console to create an instance of an ActionController?

I have a Rails 2.3.11 controller that I'm trying to debug. It looks like this:

class AppleController < ...
  # ...

  def create
    # ...
  end
end

From the log, I have some parameters p:

p = { ... }

What can I write at the console so that I can get an instance of AppleController that will work exactly like a regular instance with those parameters, and which will let me call .create?

ac = AppleController.new
# What goes here?
ac.create

(Notice that just assigning ac.params = p is not sufficient since there's no @request object, etc.) Thanks!

Upvotes: 1

Views: 1390

Answers (2)

DanSingerman
DanSingerman

Reputation: 36502

I think this is best done using the ActionController::Integration::Session class

e.g. to call the create method of your AppleController

require 'action_controller/integration'
app = ActionController::Integration::Session.new;
app.post('/apples', params) # assuming '/apples' is the path to your AppleController
puts app.response.inspect

Upvotes: 2

Eric Koslow
Eric Koslow

Reputation: 2064

If I am reading the question correctly, you can just call Apple.create(:something => "bah") in the console. That should mimic what the controller does when it gets a post request.

Upvotes: -1

Related Questions