Button_to pointed to a patch/update method doesn't works

What i'm trying to do is a button_to that point to the patch (update?) inside my controller, but it doesn't works. This is my code.

        <div>
    <%= product.picture? ? image_tag(product.picture_url, :alt => product.name) : image_tag("https://via.placeholder.com/250x250") %>
    Product: <%= product.name %><br />
    Description: <%= product.description %><br />
    Price: <%= humanized_money_with_symbol(product.price) %><br />
    Venduto da: <%= product.user.email %><br />
    <%= button_to "Remove", {controller: "orders", action: 'remove', :id => product.id}, :method=>:patch %>
    </div>

in the update method, assuming that i make it works sooner or later, i should insert a logic to remove a product from the order itself. At the moment if i go to the view page inside the index.html.erb which contains the list of the order to pay with inside the relative products added. The order in my case is a sort of cart. I get this error:

No route matches {:action=>"remove", :controller=>"orders", :id=>456}

these are my routes:

    Rails.application.routes.draw do
  root to: 'pages#home'
  devise_for :users
  resources :products
  resources :orders, only: [:show, :create, :index, :destroy, :patch]

  require "sidekiq/web"
  authenticate :user, lambda { |u| u.admin } do
    mount Sidekiq::Web => '/sidekiq'
  end
end

as you can see i added the :patch route, but it still doesn't work. I think the problem is in the first place inside my routes... I have some questions now. 1: If i want to create a link_to or a button_to helper to execute a different method from the common crud operation, do i have to create a route to that custom method? Can you make an example in case? 2: In my case, am i doing a post, a get request or a patch? (i'm trying with patch, because i'm updating my order so i guess there is where i should insert the logic to remove the product inside the order) the documentation of rails is great but about the routing, I'm very confused at the moment as soon i want to do something out of the schemas i get stuck days trying to make it works. 3: Can someone link me to a good guide with examples to understand routing? Thank you.

UPDATE i found the solution:

<%= button_to("Remove", {:controller => "orders", :action => "update", :id => product.id}, :method => :patch) %>

and i added in the route this:

  patch '/orders', action: :update, controller: 'orders'

now it works but i still have doubts about why it is working :/

1 answer

  • answered 2018-10-11 19:05 Nikitas Lozhnikov

    Briefly : Change action: 'remove' to action: 'destroy'.

    Most browsers only support GET, POST, OPTION but all the other methods are emulated by Rails using hidden form field _method.

    When you say method: 'patch' it means that in request.method in you controller's method it will be patch but in if you want to specify the exact method in the controller you should use action

    Hope that will help :)