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.

tereško
  • 58,060
  • 25
  • 98
  • 150
nulltek
  • 3,247
  • 9
  • 44
  • 94

1 Answers1

0

By default the confirm attribute in link_to uses a JavaScript window.confirm which is a simple yes/no that returns true/false.

If you want to do it all on the same page you You'll need to use JavaScript and Ajax to accomplish this. Something along the lines of adding an event handler on the Cancel link that will show a modal with a drop down. The result of this modal will then Ajax a POST request to the Rails app. There are a lot of jQuery Plugins that you can use to help you accomplish this in Rails.

The second option is the one you described which would be to use a separate action in your controller. In terms of UX I think the first option is a better route to go and isn't as scary as it sounds.

Community
  • 1
  • 1
richoffrails
  • 1,003
  • 8
  • 11
  • I like the idea of using a modal with JS/Ajax to take care of this. I'm just not sure where to start. I'm very new to JS/Ajax and I'm just getting comfortable with rails. Adding a new action seems to muddy the water a bit, I'd like things to flow from a UX point of view as well. – nulltek Sep 26 '12 at 19:28