4

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!

3
  • hi Dennis, where is your controller permit parameters action? Commented Sep 9, 2015 at 5:31
  • 1
    Actually that should be inside beer. See what you have written on controller is params.require(:beer) which means the params hash has a key named beer and the attributes to permit are inside it. In your JSON request you have this: "beer"=>{"id"=>213, "name"=>"Amber", "brewery_id"=>16} from which id is not permitted so it is showing Unpermitted parameters: id. Of the rest it don't cares whatever you are sending. Because rails will use the attributes inside beer to create/update it. Commented Sep 9, 2015 at 5:37
  • So why isn't style_ids inside of the beer hash when using JSON? Rails considers the other arguments inside that hash, but not style_ids. @huanson, I'm not sure I follow... the entire permit parameters is inside a function: def beer_params params.require(:beer).permit(:name, :brewery_id, :style_ids => []) end I call this from my update and create functions. Commented Sep 9, 2015 at 5:46

1 Answer 1

2

I finally figured out this out. Rails is using wrap_parameters so I can use {“name": “name”} instead of {“beer:{“name” : “name”}}. by default Rails wraps anything from your model’s attribute_names.

My problem was style_ids is an external join table (has_and_belongs_to_many), so that’s not in the attribute_names. I fixed this by adding this to the top of my controller: wrap_parameters :beer, include: [:style_ids, :name, :brewery_id]

The link that finally got me through this: http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.