1

I have basic Store app - I want to be able to add product to basket and then delete it using Sinatra. My adding works, but I can't make delete to work too. I had special function for that, but now I just want to see whether delete route works at all. My main app has many routes, and part I'm talking about is:

    post '/basket' do  #it works, adds to basket and redirects
      AddToBasket.new(params).call
      redirect '/'
    end


     delete "/basket/delete" do #it doesn't work at all and doesn't redirect
      basket = BASKET.find{|p| p.id == params["id"]}    
      BASKET.delete(basket)
      redirect "/"
     end

In HTML I have:

 <% basket.each do |b| %>
 <form action="basket/delete" method="post">
    <input type="hidden" name="_method" value="delete">
    <input type="hidden" name="id" value=<%= b.id %>>
    <button type="submit">Delete</button>
 </form>
<% end %>

As you can see, after clicking on "Delete" button, I'm sending "id" in my params helper.

basket = BASKET.find{|p| p.id == params["id"]} 

should find one specific item with this id and delete it from my big array BASKET. But it doesn't work, after clicking on "Delete" I'm otransferred to basket/delete page and I have an error, because post for basket/delete doesn't exist. It should redirect me to my index page. What's more, it doesn't delete my basket item, it still exists. I'll appreciate any help.

Jes
  • 323
  • 2
  • 18

1 Answers1

1

You need this component in your middleware pipeline use Rack::MethodOverride

Another way seems to be put set :method_override, true in your Sinatra::Base class

See this also

Community
  • 1
  • 1
Ursus
  • 29,643
  • 3
  • 33
  • 50
  • thank you for your answer. Where should I put it? I pasted in into my main app, but it doesn't recognize word "use" – Jes Jul 13 '16 at 09:19
  • thank you, the thing is now it redirects properly, but it still doesn't delete my basket item – Jes Jul 13 '16 at 09:26
  • I tried different options. When I do it like in my first post, but with set :method_override, it redirects me properly, but doesn't delete an item. When I change route to basket/delete/:id as you suggested (and change form accordingly), after clicking on "Delete" button Sinatra gives me error page - it wants me to have action delete "basket/delete/" set. I did so - so now I have both delete "basket/delete/:id" and delete "basket/delete/". Redirects workd now, but still it doesn't delete anything. – Jes Jul 13 '16 at 09:39
  • No I'm sorry I was wrong, you can pass your parameter either in the path or as parameter in your form. But I have no enough information to help you, I don't know which is the error. – Ursus Jul 13 '16 at 09:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117184/discussion-between-ursus-and-jes). – Ursus Jul 13 '16 at 09:47
  • I hardcoded it: basket = BASKET.find{|p| p.id == 1 } and it works - so it's now deleting, the problem is somehow params[:id] is wrong value. Do you know how can I print my params? I tried like: delete "basket/delete" print params[:id] etc but it doesn't print them in my console – Jes Jul 13 '16 at 10:01