implementing a cancel button

How can you implement a cancel button that updates the status of orders on a listing to "cancelled". I want to be able to carry over the order.id to the controller action that changes the status to cancelled.

Posted over 3 years ago by Will
Posted over 3 years ago by Will

Ive tried using this:

<%= form_tag guest_cancel_path, method: :post do |f| %>
<input type="hidden" name="order_id" value="<% @order.id %>" ></input>
<%= submit_tag "Cancel ",class: "cancel-button btn wide" %>
<% end %>

In my controller, I have:

def guest_cancel
@user = current_user
@order = Order.find(params[:order_id])
redirect_to guest_requests_path, notice: " the order: #{@reservation} by user -> #{@user} has been cancelled, "

end


then in my routes:

post 'guest_cancel' => 'orders#guest_cancel'

1
Posted over 3 years ago by Alex Yang

Hi Will, there are two main steps to make this change - see below for a high-level outline. Hope this helps as a starting point for your customizations!

1) Add a new field (e.g. 'status') to your Order table - this field will be used to track whether an order is 'active' or 'cancelled'. Make sure the default value is 'active'.

2) Add a button to your view page (which one depends on where you want the button to appear) that's tied to a new action (e.g. cancel) in your OrdersController file. The code for this action should set the status field to 'cancelled' and use the Stripe API to refund any charges.

1
Posted over 3 years ago by Will

Ive tried using this:

<%= form_tag guest_cancel_path, method: :post do |f| %>
<input type="hidden" name="order_id" value="<% @order.id %>" ></input>
<%= submit_tag "Cancel ",class: "cancel-button btn wide" %>
<% end %>

In my controller, I have:

def guest_cancel
@user = current_user
@order = Order.find(params[:order_id])
redirect_to guest_requests_path, notice: " the order: #{@reservation} by user -> #{@user} has been cancelled, "

end


then in my routes:

post 'guest_cancel' => 'orders#guest_cancel'

1
Posted over 3 years ago by Will

But am getting a no method error for nil class because I think creating a form within the order show page prevents the id to be passed into the form, maybe because its not a global variable, but I dont know how else to pass the order.id into the controller cancel action

1
Posted over 3 years ago by Alex Yang

What line of code is the NoMethodError triggering on? The best way to pass the order id is to make sure it's included as part of the URL. Here's a good Stack Overflow link illustrating this approach:

http://stackoverflow.com/questions/6480713/how-to-get-the-post-id-in-rails

1
Posted over 3 years ago by Will

I was getting a NoMethodError for NilClass on the order.id which was showing empty params on the HTML generated but I fixed it by using hidden_field_tag instead

1