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"

Community
  • 1
  • 1
stevec
  • 41,291
  • 27
  • 223
  • 311

2 Answers2

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
kcsujeet
  • 465
  • 2
  • 8
  • Really appreciate your response. can you please reword to use the model name (model) and attribute names (name, age) used in the question? – stevec Jul 10 '19 at 09:34
  • can you please update the question with what you're sending from front-end at the moment.. your request body. – kcsujeet Jul 10 '19 at 09:40
  • I have updated the question to show the curl request – stevec Jul 10 '19 at 09:44
  • I tried it. I get `ActionDispatch::Http::Parameters::ParseError (765: unexpected token at 'model: {name_and_age: .. etc`, so I tried as suggested [here](https://stackoverflow.com/questions/49053954/testing-post-actiondispatchhttpparametersparseerror-765), 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'` – stevec Jul 10 '19 at 10:07
  • 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. – kcsujeet Jul 10 '19 at 10:33
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

kishore cheruku
  • 511
  • 2
  • 11