0

I have an application, which contains calls. I want to be able to cancel the call and supply a reason for the call cancellation. So far I have my cancel action working in the controller, but I'm trying to figure out how to expand it so before it posts "cancel" to the call_status field it will also populate a cancel_reason field based on a drop down.

Here's what I have so far:

view code: cancel button

<%= link_to 'Cancel', 
    cancel_call_path(call), 
    confirm: 'Are you sure you want to cancel the call?', 
    :method => :post, 
    :class => 'btn btn-danger btn-mini' %>


controller code: cancel action

def cancel
        @call = Call.find(params[:id])

        attrs = {
          call_status: 'cancel', 
          incharge_id: @call.units.first.incharge_id, 
          attendant_id: @call.units.first.attendant_id
        }
        attrs.merge!({ incharge2_id: @call.units.second.incharge_id, attendant2_id: @call.units.second.attendant_id }) if @call.units.count == 2

        if @call.update_attributes(attrs)
          @call.units.each do |unit|
             CallMailer.cancel_call(unit.incharge, @call).deliver
             CallMailer.cancel_call(unit.attendant, @call).deliver
           end
         redirect_to calls_url, :notice => "Call was successfully cancelled"
        else 
          redirect_to calls_url, :error => "Whoops."
        end
      end

I want either the confirmation pop-up shown, with the reason for cancellation, or the cancel action tied to a different view with a small form, that includes a reason.

4

1 回答 1

0

默认情况下,confirmin 属性link_to使用 JavaScript window.confirm,它是一个简单的是/否,返回 true/false。

如果您想在同一个页面上完成所有操作,您将需要使用 JavaScript 和 Ajax 来完成此操作。类似于在 Cancel 链接上添加事件处理程序的内容,它将显示带有下拉菜单的模式。然后,此模式的结果将向 Rails 应用程序发送一个 POST 请求。有很多 jQuery 插件可以用来帮助你在 Rails 中完成这项工作。

第二个选项是您描述的选项,即在控制器中使用单独的操作。在用户体验方面,我认为第一个选择是一条更好的路线,并不像听起来那么可怕。

于 2012-09-26T19:02:51.490 回答