2014-12-06 208 views
0

我有一个链接更新资源创建新资源:从另一个控制器

<%= 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 

我想链接到其他地方也创建一个新的资源:报销。 报销将收到预订的编号。

我该怎么做?

谢谢

回答

0

使用after_update回调。

在模型中,在此cancel方法写的是:

after_update :create_new_resource 

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

THX对您有所帮助。我在我的模型中添加了after_update,并在我的控制器中添加了def,但是它为#说明了“未定义方法'create_new_resource'”您有想法吗? – Devlesch 2014-12-07 05:31:06

+0

@Devlesch,请问您可以使用Reservation模型创建一个要点吗?所以我可以看看? – 2014-12-07 07:32:09

+0

https://gist.github.com/devlesch/536a009de5a05b73d4ab你还需要别的东西吗? – Devlesch 2014-12-07 15:14:07