Skip to main content
added 20 characters in body
Source Link
Pavan
  • 33.6k
  • 8
  • 53
  • 78

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 ERBERB, it looks like this:

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! 
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!

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:

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! 

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!

Source Link
Dennis
  • 1.7k
  • 2
  • 12
  • 15

How to POST/PUT array param in Rails

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:

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!