0
JSONをコントローラの作成メソッドに渡しています。この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
スキーマ:ActiveRecordので
create_table "people", force: :cascade do |t|
t.string "name"
t.string "sport"
t.string "mood"
end