Ruby on rails 当我知道服务期限时,如何计算预约的结束时间?

Ruby on rails 当我知道服务期限时,如何计算预约的结束时间?,ruby-on-rails,datetime,nested-forms,Ruby On Rails,Datetime,Nested Forms,我有两种型号,服务和预约。一个服务可以有很多预约 我有一个嵌套表单供用户预订约会 如何自动计算约会的结束时间?可以理解,我不希望依赖用户根据他们选择的服务的长度输入结束时间 目前我的控制器看起来像这样 class AppointmentsController < ApplicationController before_action :set_appointment, only: [:show, :edit, :update, :destroy] before_action :lo

我有两种型号,服务和预约。一个服务可以有很多预约

我有一个嵌套表单供用户预订约会

如何自动计算约会的结束时间?可以理解,我不希望依赖用户根据他们选择的服务的长度输入结束时间

目前我的控制器看起来像这样

class AppointmentsController < ApplicationController
  before_action :set_appointment, only: [:show, :edit, :update, :destroy]
  before_action :load_services, only: [:new, :edit]
  after_filter :end_calculate, only: [:create, :update]

  [...]

  # POST /appointments
  # POST /appointments.json
  def create
    @appointment = Appointment.new(appointment_params)

    respond_to do |format|
      if @appointment.save
       # redirect_to root_url
        format.html { redirect_to @appointment, notice: 'Appointment was successfully created.' }
        format.json { render :show, status: :created, location: @appointment }
      else
        format.html { render :new }
        format.json { render json: @appointment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /appointments/1
  # PATCH/PUT /appointments/1.json
  def update
    respond_to do |format|
      if @appointment.update(appointment_params)
        format.html { redirect_to @appointment, notice: 'Appointment was successfully updated.' }
        format.json { render :show, status: :ok, location: @appointment }
      else
        format.html { render :edit }
        format.json { render json: @appointment.errors, status: :unprocessable_entity }
      end
    end
  end

  [...]

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_appointment
      @appointment = Appointment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def appointment_params
      params.require(:appointment).permit(:start_time, :end_time, :note, :service_id)
    end

    def load_services
      @services = Service.all.collect {|service| [ service.title, service.length, service.id] }
    end

    def end_calculate
      @appointment.end_time = @appointment.start_time + @service.length.minutes
      @appointment.end_time.save
    end

end
类指定控制器
因此,经过讨论后,解决方案是从控制器中删除
end\u calculate
方法,并将
end\u time
方法添加到约会类:

  def end_time
    end_time = self.start_time + self.service.length.minutes
  end

长度是什么类型的?它是一个整数。该数字表示服务将持续多少分钟。所以可能是30,60,90等等。我假设开始时间是一种时间,对吗?正是我需要的。