samedi 21 octobre 2017

Cant update relations on Update action in rails controller

When updating a resource called JobsI want to copy some of the values from this update to some resources connected to this resource

Jobs has many Hours each hour has a period with 2 set of times. So if i update the job with these times i want them copied down to the periods

I tried this code in different variants but the periods entities dont get updated.

  def update
@job = Job.find(params[:id])

respond_to do |format|
  if @job.update_attributes(params[:job])

    @job = Job.find(params[:id])

    @job.hours.each do |hour|
      logger.debug params[:job][:start1]
      logger.debug params[:job][:end1]
      hour.periods.first.start_time = params[:job][:start1]
      hour.periods.first.end_time = params[:job][:end1]
      hour.periods.last.start_time = params[:job][:start2]
      hour.periods.last.end_time = params[:job][:end2]
    end

    @job.save

    format.html { redirect_to @job, notice: 'Job was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @job.errors, status: :unprocessable_entity }
  end
end

end

Aucun commentaire:

Enregistrer un commentaire