0

what is the best way to implement a cancel button on an orders show page. The cancel button simply updates the order's status attributes to "cancelled" in a controller. I would like to carry over the order.id to the controller as each user has many orders.I am currently getting an undefined method 'id' for nil:Nilclass which makes me think the @order.id is not being passed into the hidden-field. Not sure what am doing is the best way to pass the order.id into the controller&welcome any ideas for a better solution

<div>


    <% @orders.each do |order| %>
        <%= order.id %>
        <%= order.total %>
        <%= order.user.name %>

        //lots of boring stuff then at the bottom of the page

        <%= form_tag guest_cancel_path, method: :post do |f| %> 
        <input type="hidden" name="order_id" value="<% order.id %>" >  
        <%= 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]) 
    @order.update(status: 'cancelled')
    redirect_to guest_requests_path, notice: " the order: #{@order} by user -> #{@user} has been cancelled, " 

end 

then in my routes:

post 'guest_cancel' => 'orders#guest_cancel' 
Wil
  • 31
  • 7

2 Answers2

1

It seems you missed to output it "<%= order.id %>"

you would need to add this the controller too: @order.update(status: 'cancalled')

  • I forgot to type that on here, thanks, but I am getting an undefined method 'id' for nil:nilClass even after outputting the order.id – Wil Jun 04 '16 at 02:33
  • Just updated the answer .. I thinks it should've been order.id directly .. (inside loop) .. is it working ? – Abdullah Barrak Jun 04 '16 at 02:36
  • No it didnt work, I think the problem is that the id is not being passed into the input field because the order.id acts as a local variable that cannot be accessed inside the form. Thanks for trying, though – Wil Jun 04 '16 at 02:43
  • okay. how about assigning it at each iteration `<% @current_order_id = order.id %>` before entering the form_tag block. Then use `@current_order_id` instead. – Abdullah Barrak Jun 04 '16 at 02:54
  • As he said originally, you need an equals sign: `<%= order.id %>` without an equals sign in there, Erb evaluates the order id, but doesn't include it in the HTML output – philomory Jun 04 '16 at 23:24
1

If you have relation between listening and orders then you should write <% listing.orders.each do |order| %> so, orders instead of order.

Jovica Šuša
  • 622
  • 3
  • 8
  • Thanks for the catch. That was a typing error here, but I changed my code and updated the question from looping through each user's listings and printing all the orders in each listing to just checking all the orders where the user is a guest <% @orders.each do |order| %> but am back to the same problem of not being able to pass the order.id to the hidden-field attribute – Wil Jun 04 '16 at 17:48
  • You're welcome, I'm not sure what is exactly happening, can you use something like pry or debugger in your controller to check what's inside params hash? https://github.com/nixme/pry-debugger – Jovica Šuša Jun 04 '16 at 18:00
  • It may be that you have to access order_id like this params[:order][:order_id] – Jovica Šuša Jun 04 '16 at 18:06
  • I checked the hash is empty. I don't think the problem is at the controller. I think the order can not be passed into the form because its not a global variable that can be accessed inside the form. I will probably have to figure out set the order.id to a global variable then use it inside the form like Abdullah suggested or remove the form completely and try to do an ajax post action to send the id as json or something to the controller which will be too much complexity just to send a simple id to a controller – Wil Jun 04 '16 at 18:28
  • I would suggest you to first try with form_for instead of form_tag and use hidden_field instead of regular input with type="hidden". http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-hidden_field – Jovica Šuša Jun 04 '16 at 18:38
  • This may be helpful http://stackoverflow.com/questions/18205875/how-to-access-hidden-field-in-controller-in-ruby-on-rails – Jovica Šuša Jun 04 '16 at 20:07
  • Hey, thanks for the last link, it was helpful and for your time and effort. All I had to was use a hidden_field_tag instead. I will upvote your answer – Wil Jun 04 '16 at 22:06