从另一个控制器创建新资源

时间:2014-12-06 22:52:17

标签: ruby-on-rails ruby-on-rails-4

我有一个更新资源的链接:

<%= link_to "Cancel", doctor_reservation_cancel_path(reservation.doctor, reservation), method: :put %>

该链接将更新预订并将其状态设置为取消。

  def cancel
@reservation = Reservation.find(params[:reservation_id])
respond_to do |format|
  if @reservation.update(:status_id => 4, :canceleddate => DateTime.now)
    format.html { redirect_to dashboard_path, notice: 'Your reservation was succefully canceled.' }
    format.json { render :show, status: :ok, location: @reservation }
  else
    format.html { render :edit }
    format.json { render json: @reservation.errors, status: :unprocessable_entity }
  end
end
end

我希望该链接也可以在其他地方创建新资源:报销。 该报销将收到预订的身份。

我该怎么做?

谢谢,

1 个答案:

答案 0 :(得分:0)

使用after_update回调。

在模型中,编写此cancel方法:

after_update :create_new_resource

def create_new_resource
  Reimbursement.create(your_params) if status_changed? && status == 'Cancel'
end