How do you pass parameters to a controller method when you invoke it in the Rails console?
Asked Answered
M

5

13

I'm using Rails 5. I have this controller ...

class MyObjectsController < ApplicationController

  def create
    my_object = MyService.build(create_params)

I would like to call the create method in the rails console but I get this error ...

irb(main):007:0> MyObjectsController.new.create(:id => "abc")
Traceback (most recent call last):
        2: from (irb):7
        1: from app/controllers/my_objects_controller.rb:4:in `create'
ArgumentError (wrong number of arguments (given 1, expected 0))

How do I pass parameters to my controller method?

Matelot answered 1/10, 2019 at 20:25 Comment(0)
M
27

You're getting this error because the create method doesn't receive any argument. In order to use the create action properly you need to pass the ActionController::Parameters to your controller's instance:

c = MyObjectsController.new
c.params = ActionController::Parameters.new(id: "abc")
c.create # It will not work if this controller uses authentication
Meijer answered 10/10, 2019 at 16:36 Comment(4)
This "answer" would be better with some context. Also this new({id: "abc"}) can be just this new(id: "abc"). Is always better typing less.Vocalist
The controller function indeed runs, but when it tries to render, it throws DelegationError. Any way to solve it?Bracteate
And what to do in that case where it uses authentication?Rouge
Roughly speaking you could write c.request = ActionDispatch::Request.new({}) and c.request.headers['Authorization'] = 'YOUR_AUTH_TOKEN'. But depending on how you're authenticating your users, maybe this isn't enough.Meijer
T
1

If the requirement is to call action method of a controller having request method as POST via rails console and pass parameters in it, then it can be done via following commands

# Request any of the application resource, for example root url to get authenticity token
app.get '/'
token = app.session[:_csrf_token]

# parameters to send
parameters = { my_object: { field_one: 'foo', field_two: 'bar' }, authenticity_token: token }

# Call controller method
app.post '/my_objects', params: parameters
Tricolor answered 7/10, 2019 at 12:33 Comment(0)
P
0

You can try

app.post "?create_params[id]=abc"

For example

app.post '/users?user[name]=test&user[age]=20'

will pass the following {"user"=>{"name"=>"test", "age"=>"20"}}

Psychological answered 7/10, 2019 at 10:55 Comment(0)
I
0

params in Rails can't access everywhere. Params in Rails can only access from class inherited from ActionController::Base, so in Rails console you cannot access your params, but you can access params via a public method.

Isotherm answered 10/10, 2019 at 9:11 Comment(0)
S
0

You can define your controller action as follows:

def my_actions(myparams=nil)
  @loginid = myparams.nil? ? params[:login_id] : myparams[:login_id] 
end

This way you can test your controller in rails console by passing your params.

Swahili answered 13/1, 2021 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.