R
R
Roman Mirilaczvili2017-01-16 14:44:33
API
Roman Mirilaczvili, 2017-01-16 14:44:33

Do I misunderstand the purpose of param.require?

In the controller, you need to make sure that the parameters are present. If not, then in the API, the ActionController::ParameterMissing handler, as I understand it, must independently return a response about missing parameters.
I test like this:

ActionController::Parameters.new(foo1: 'bar1', foo2: 'bar2').require([:foo1, :foo2])

ActionController::ParameterMissing: param is missing or the value is empty: [:foo1, :foo2]

Which is illogical, since the parameters are passed. What am I doing wrong?
I'm looking for a concise alternative to simple checks in a controller like this:
(render json: {status: "Error", message: "Missing parameter 'foo1'"}; return) unless param.has_key?(:foo1)
(render json: {status: "Error", message: "Missing parameter 'foo2'"}; return) unless param.has_key?(:foo2)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Beloshitsky, 2017-01-16
@2ord

requiredoes not support arrays or multiple arguments. Call one by one.

params = ActionController::Parameters.new(foo1: 'bar1', foo2: 'bar2')
params.require(:foo1)
params.require(:foo2)

Or use map.

V
Vladislav Kopylov, 2017-01-16
@kopylov_vlad

Maybe you mixed up require and permit?
apidock.com/rails/ActionController/Parameters/require
apidock.com/rails/ActionController/Parameters/permit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question