rails strong parameter not accepting array of hashes

Gagan picture Gagan · Aug 4, 2014 · Viewed 9.1k times · Source

I have a group controller which accepts array of hashes as parameter for POST request for create action

  def create
    response = Group.create(current_user_id, group_params)
    render json: response
  end

  def group_params
    params.require(:group).permit(:group_name, :group_title, group_members: [])
  end

But its not accepting the group_members parameter via strong parameter

Here is what the incoming request with params looks like in my app

Started POST "/groups" for 127.0.0.1 at 2014-08-04 08:25:37 +0545
Processing by GroupsController#create as JSON
  Parameters: {"group"=>{"group_name"=>"Fourth group", "group_title"=>"fourth tester", "group_members"=>[{"id"=>"0833be3c-17db-11e4-904b-3f662703cb5b", "darknet_accountname"=>"@ckgagan", "access_level"=>"Write"}]}}
Unpermitted parameters: group_members
Completed 200 OK in 10ms (Views: 0.2ms)

I have seen many posts which says that adding group_members : [] inside permit will work but its not working in my case.

Answer

Gagan picture Gagan · Aug 4, 2014

Solved the problem by adding the group_members field inside permit args.

def group_params
   params.require(:group).permit(:group_name, :group_title, group_members: [:id, :darknet_accountname, :access_level])
end

After this there was no complaint about unpermitted parameters within group_members.