我正在将JSON传递给我的控制器创建方法。我试图将此JSON传递到我的模型/数据库中。但我一直收到错误:
When assigning attributes, you must pass a hash as an argument.
我没有传递哈希值吗?
JSON:
{
"people": [
{
"name": "Tom",
"sport": "Ski",
"mood": "Happy",
},
{
"name": "Robbie",
"sport": "Swim",
"mood": "sad",
},
{
"name": "Ted",
"sport": "Running",
"mood": "Sleepy",
},
]
}
控制器:
class PeopleController < ApplicationController
respond_to :json
def create
new_record = people_params
records = People.new(new_record)
records.save
end
private
def people_params
params.permit(people: [:name, :sport, :mood]).require(:people)
end
end
架构:
create_table "people", force: :cascade do |t|
t.string "name"
t.string "sport"
t.string "mood"
end