Strong attributes requiring an attribute

Is it possible to tag one hash attribute as needed using strong parameters?

This input is as:

{
  "example" => {
    "optional": 1234,
    "required": 5678
   }
}

Standard examples of strong parameters are:

params.require(:example).permit(:optional, :required)

Given that you may require certain parameters, I thought the following would work:

params.require(:example).require(:required)
params.require(:example).permit(:optional)

I tried:

params.require(:example => [ :required ]).permit(:optional)
params.require(:example).permit(:optional)
params[:example].require(:required)

And all that I can come up with.

Does anyone know if this is possible?

+5
source share
2 answers

Greg!

I had the same question, but in the end I found that its not a relevant question.

Look, here is the source code for the require method in strong_parametersgem:

def require(key)
  self[key].presence || raise(ActionController::ParameterMissing.new(key))
end

, , "" params. . , , . , validates_presence_of . , . :

http://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html#performing-custom-validations

+5

,

def example_params
  params.require(:example)
  params[:example].require(:required)
  params.require(:example).permit(:required, :optional)
end

, :example. , :required :example. , , :optional.

0

All Articles