1

here is my ajax request:

$.ajax({
url: "/menu_items",
method: "post",
data: {
    menu_items: [
        {"id":3,"content":"first"},
        {"id":4,"content":"first"},
        {"id":5,"content":"second"}
    ]
  }
})

I want to permit id and content in the menu_items array, and I read the docs.

so I try

params.require(:menu_items).permit({menu_items: [[:id, :content]]})

or

params.require(:menu_items).permit({menu_items: [:id, :content]})

but doesn't work, so what should I do?

update: here is my controller:

class MenuItemsController < ApplicationController
  def create
    puts "----------------"
    puts menu_items_param
    puts "-----------"
  end

  private
  def menu_items_param
    params.require(:menu_items).permit(menu_items: [:id, :content])
  end
end

In console, it print

Processing by MenuItemsController#create as */*
 Parameters: {"menu_items"=>{"0"=>{"id"=>"3", "content"=>"first"}, "1"=>{"id"=>"4", "content"=>"first"}, "2"=>{"id"=>"5", "content"=>"second"}}}
----------------
Unpermitted parameters: 0, 1, 2
{}
-----------
ChiangDi
  • 85
  • 1
  • 7

1 Answers1

1
params.require(:menu_items).permit(:id, :content)

Try this.

Vrushali Pawar
  • 3,753
  • 1
  • 13
  • 22