2012-09-26 56 views
0

我有一個應用程序,其中包含調用。我希望能夠取消通話並提供通話取消的原因。到目前爲止,我有我的取消行動在控制器中工作,但我試圖找出如何擴大它,所以在它發佈「取消」到call_status字段之前,它也將基於下拉列表填充cancel_reason字段。Rails取消確認信息原因

這是我到目前爲止有:

視圖代碼:取消按鈕

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


控制器代碼:取消動作

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 

我想任一確認彈出所示,用取消的原因,或取消動作綁在不同視圖中,帶有小的形狀,即包括一原因。

+0

問題是什麼? –

回答

0

默認情況下,link_to中的confirm屬性使用JavaScript window.confirm,這是一個簡單的是/否返回true/false。

如果您想在同一頁面上完成所有操作,您需要使用JavaScript和Ajax來完成此操作。在「取消」鏈接上添加事件處理程序的某些內容將顯示帶有下拉列表的模式。這個模式的結果是Ajax將一個POST請求發送到Rails應用程序。您可以使用There are a lot of jQuery Plugins來幫助您在Rails中完成此操作。

第二個選項是你所描述的那個,它將在你的控制器中使用一個單獨的動作。就用戶體驗而言,我認爲第一種選擇是更好的途徑,並不像聽起來那麼可怕。

+0

我喜歡用JS/Ajax模式來處理這個問題。我只是不知道從哪裏開始。我對JS/Ajax非常陌生,我只是習慣使用rails。添加一個新的動作似乎讓水有點渾濁,我希望事情也從UX的角度來看。 – nulltek