0

I made a shopping cart and each product has a delete link that generates the following message when clicked:

Routing Error No route matches [GET] "/order_items/8"

The only change between the errors generated by each link is the order_item id.

The code is the following:

<%= link_to "Delete", order_item, { data: { confirm: "Are you sure you wish to delete the product '#{order_item.product.name}' from your cart?" }, method: :delete, remote: true, class: "btn btn-danger" } %>

This is the generated link:

<a data-confirm="Are you sure you wish to delete the product 'Name' from your cart?" class="btn btn-danger" data-remote="true" rel="nofollow" data-method="delete" href="/order_items/8">Delete</a>

This is the relevant data in routes file:

resources :order_items, only: [:create, :update, :destroy]

When clicked, the alert pops up but the log shows this:

DELETE http://localhost:3000/order_items/11 422 (Unprocessable Entity)

What am I missing for the code to work.

4
  • Does it really redirect or did you forget to import jquery-ujs (which sends the DELETE http verb) Commented Jul 29, 2016 at 20:57
  • It is being called from application.js as follows //= require jquery_ujs and the gem jquery-rails is called on the Gemfile. I added the generated HTML in case something is wrong with it. Commented Jul 29, 2016 at 21:19
  • Please add your logs too. Commented Jul 29, 2016 at 21:45
  • @RaVeN you were, right I was not including the application javascript in my layout now the javascript confirmation appears but the log shows this: " DELETE localhost:3000/order_items/8 422 (Unprocessable Entity)" Commented Jul 29, 2016 at 21:51

2 Answers 2

1

I was able to fix this behavior by adding the following line to my layout's head:

<%= csrf_meta_tags %>
Sign up to request clarification or add additional context in comments.

Comments

0

An alternative may be to trade out [:delete, order_item] for order_item.

As in:

<%= link_to "Delete", [:delete, order_item], { data: { confirm: "Are you sure you wish to delete the product '#{order_item.product.name}' from your cart?" }, remote: true, class: "btn btn-danger" } %>

1 Comment

This results in a No Method Error undefined method delete_order_item_path' for #<#<Class:0x0055af888929b0>:0x0055af8a15c680>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.