Вложенные API-интерфейсы Rails 5 разрешены false
class TimeSheet < ApplicationRecord
has_many :job_times, inverse_of: :time_sheet
accepts_nested_attributes_for :job_times, :allow_destroy => true
end
class JobTime < ApplicationRecord
belongs_to :time_sheet, optional: true
end
Контроллер
def create
puts "Job time #{params.dig(:job_times)}"
@time_sheet = TimeSheet.create!(time_sheet_params)
if @time_sheet.save
render json: @time_sheet, status: :created, location: @time_sheet
else
render json: @time_sheet.errors, status: :unprocessable_entity
end
end
def time_sheet_params
params.require(:time_sheet).permit(:first_name, :job_date,
job_times_attributes: [:job_id, :job_department, :job_category,
:job_time, :is_overtime ])
end
Параметры:
{"first_name"=>"GG", "job_date"=>"2018-05-22T00:50:19.000Z", "job_times"=>[{"job_id"=>"18001", "job_department"=>"CAD", "job_category"=>"Finishing", "job_time"=>"5", "is_overtime"=>""}, {"job_id"=>"18032", "job_department"=>"test", "job_category"=>"Finishing", "job_time"=>"2", "is_overtime"=>""}], "time_sheet"=>{"first_name"=>"GG", "job_date"=>"2018-05-22T00:50:19.000Z"}}
Job time [<ActionController::Parameters {"job_id"=>"18001", "job_department"=>"CAD", "job_category"=>"Finishing", "job_time"=>"5", "is_overtime"=>""} permitted: false>, <ActionController::Parameters {"job_id"=>"18032", "job_department"=>"test", "job_category"=>"Finishing", "job_time"=>"2", "is_overtime"=>""} permitted: false>]
Спасибо за помощь.