My controller methods work great for my ERB forms, but aren't accepting arrays as JSON parameters. I have an array of ids called "style_ids" that gets stripped out from JSON.
Permitted parameters:
params.require(:beer).permit(:name, :brewery_id, :style_ids => []) When posted from ERB, it looks like this:
all params: {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>”…”, "beer"=>{"name"=>"Amber", "style_ids"=>["", "1"], "brewery_id"=>"16"}, "commit"=>"Submit", "action"=>"update", "controller"=>"beers", "id"=>"213"} permitted params: {"name"=>"Amber", "brewery_id"=>"16", "style_ids"=>["", "1"]} When I submit via JSON though, it looks like this:
params: {"id"=>"213", "name"=>"Amber", "style_ids"=>["1", "22"], "brewery_id"=>16, "action"=>"update", "controller"=>"beers", "beer"=>{"id"=>213, "name"=>"Amber", "brewery_id"=>16}} Unpermitted parameters: id permitted params: {"name"=>"Amber", "brewery_id"=>16} I'm guessing this has something to do with Rails showing style_ids inside the beer hash in my regular request. What am I doing wrong that's keeping this from showing up in my JSON request? The JSON is really simple: {"id":213,"name":"Amber","style_ids":["1","22"],"brewery_id":16}
I've already read a lot of documentation about this and feel like I'm doing it the right way...hopefully I'm just missing something. Thanks!
beer. See what you have written on controller isparams.require(:beer)which means the params hash has a key namedbeerand the attributes to permit are inside it. In yourJSONrequest you have this:"beer"=>{"id"=>213, "name"=>"Amber", "brewery_id"=>16}from whichidis not permitted so it is showingUnpermitted parameters: id. Of the rest it don't cares whatever you are sending. Because rails will use the attributes insidebeerto create/update it.def beer_params params.require(:beer).permit(:name, :brewery_id, :style_ids => []) endI call this from my update and create functions.