mercredi 27 janvier 2016

Ruby on Rails - custom PATCH action for form_for

I'm working on a Spree e-commerce store built on Ruby on Rails and want a custom action where a user can mark their order as complete straight from the checkout page without going through delivery etc. I've overridden all the checkout steps but cannot get the 'Checkout' button to complete the order by sending the order to a custom action in the Orders controller.

I'd like to think I've ticked off all the boxes: created a patch action in routes.rb and checked rake routes to make sure the route exists. But it's still telling me there is no route.

The cart page won't even load before I submit anything, with the following error. I've spent all day trying to fix this so any ideas would be great....

The error:

No route matches {:action=>"complete", :controller=>"spree/orders", :method=>:patch}

Routes.rb:

resources :orders do
   member do
     patch 'complete', to: 'orders#complete'
   end
 end

Rake routes:

        Prefix Verb   URI Pattern                    Controller#Action
       spree        /                              Spree::Core::Engine
complete_order PATCH  /orders/:id/complete(.:format) orders#complete
        orders GET    /orders(.:format)              orders#index
               POST   /orders(.:format)              orders#create
     new_order GET    /orders/new(.:format)          orders#new
    edit_order GET    /orders/:id/edit(.:format)     orders#edit
         order GET    /orders/:id(.:format)          orders#show
               PATCH  /orders/:id(.:format)          orders#update
               PUT    /orders/:id(.:format)          orders#update
               DELETE /orders/:id(.:format)          orders#destroy

HTML:

<%= form_for :order, url: {action: 'complete', method: :patch} do |f| %>
  <% f.submit %>
<% end %>

I haven't created the controller yet but it would be:

def complete
  # mark order as complete
  # redirect to confirmation page
end

Would really appreciate any help. Thanks

Aucun commentaire:

Enregistrer un commentaire