1

I have a basic API built by rails new my_api --api, then rails g scaffold model name age.

I would like to alter the model_params method to accept a JSON array. How can I do this?

The current method is the default:

   def model_params
      params.require(:model).permit(:name, :age)
   end

What I've tried

I have tried this:

    def model_params
      params.require(:model).permit?([:name, :age])
    end

But I get

Completed 400 Bad Request in 10ms (ActiveRecord: 10.3ms)


  
ActionController::ParameterMissing (param is missing or the value is empty: model):
  
app/controllers/models_controller.rb:83:in `model_params'
app/controllers/models_controller.rb:29:in `create'

Note

The curl request I make is

curl -H "Accept: application/json" -H "Content-type: application/json" -d '[{"name": "Fred", "age": "5"}, {"name": "Fred", "age": "5"}, {"name": "Fred", "age": "5"}]' "http://localhost:3000/models"

2 Answers 2

0

possibility which I think I like even better would be to allow permit! to take an optional parameter, to allow this.

params.require(:model).permit(:name).permit!(:data)

You can refer this This

Sign up to request clarification or add additional context in comments.

Comments

0

your strong param requires an object called model which has keys name and age so you need to make your request body in that format.

        { "model" : { 
                "name": "Ross", 
                "age": "25"
          } 

but what you want is

    [{"name": "Fred", "age": "5"}, 
{"name": "Fred", "age": "5"},
 {"name": "Fred", "age": "5"}]

so this should work.

  {"model": {
    "name_and_age": [
    {"name": "Fred", "age": "5"}, 
    {"name": "Fred", "age": "5"},
     {"name": "Fred", "age": "5"}
    ]
    }

}

for this to work you'll have to change remove :name, :age from permit and add name_and_age instead somethings like.

def model_params
      params.require(:model).permit(:name_and_age)
    end

5 Comments

Really appreciate your response. can you please reword to use the model name (model) and attribute names (name, age) used in the question?
can you please update the question with what you're sending from front-end at the moment.. your request body.
I have updated the question to show the curl request
I tried it. I get ActionDispatch::Http::Parameters::ParseError (765: unexpected token at 'model: {name_and_age: .. etc, so I tried as suggested here, now I get Completed 400 Bad Request in 1ms (ActiveRecord: 0.2ms) and ActionController::ParameterMissing (param is missing or the value is empty: model): app/controllers/models_controller.rb:83:in model_params'` and app/controllers/models_controller.rb:29:in create'`
check edited answer again. and i suggest using postman instead of curl. also if you decide to go the way i'm suggesting then you'll have to process the request data in a format you want. maybe there are better ways to do it. not sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.