Best practice to add custom route to the Spree application

868 views
Skip to first unread message

Arup Rakshit

unread,
Apr 13, 2015, 8:47:49 AM4/13/15
to spree...@googlegroups.com
I would like to add a new route to a Spree same as this action - https://github.com/spree/spree/blob/master/api/config/routes.rb#L78

I did it using :

  put '/shipments/:id/deliver', :to => 'spree/api/v1/shipments#deliver', :as => "shipment_deliver"

But, while doing `rake routes | grep ship`, getting the output as :

                      shipment_deliver PUT    /shipments/:id/deliver(.:format)                          spree/api/v1/shipments#deliver
        checkout_shipping_static_pages GET    
/static_pages/checkout_shipping(.:format)                 static_pages#checkout_shipping
       
#......
                                                  PUT    
/admin/shipping_categories/:id(.:format)                                    spree/admin/shipping_categories#update
                                                  DELETE
/admin/shipping_categories/:id(.:format)                                    spree/admin/shipping_categories#destroy
                               ready_api_shipment PUT    
/api/shipments/:id/ready(.:format)                                          spree/api/shipments#ready {:format=>"json"}
                                ship_api_shipment PUT    
/api/shipments/:id/ship(.:format)                                           spree/api/shipments#ship {:format=>"json"}
                                 add_api_shipment PUT    
/api/shipments/:id/add(.:format)                                            spree/api/shipments#add {:format=>"json"}
                              remove_api_shipment PUT    
/api/shipments/:id/remove(.:format)                                         spree/api/shipments#remove {:format=>"json"}
                                    api_shipments POST  
/api/shipments(.:format)                                                    spree/api/shipments#create {:format=>"json"}
                                     api_shipment PATCH  
/api/shipments/:id(.:format)                                                spree/api/shipments#update {:format=>"json"}
                                                  PUT    
/api/shipments/:id(.:format)                                                spree/api/shipments#update {:format=>"json"}

I am not seeing the `{:format=>"json"}` option with my newly added controller route. Why so ?

BTW, What is the best practice to add custom route to the Spree application ?

Arup Rakshit

unread,
Apr 13, 2015, 8:55:58 AM4/13/15
to spree...@googlegroups.com
Well, I have added the route as now :

  put '/shipments/:id/deliver', :to => 'spree/api/v1/shipments#deliver', constraints: { format: 'json' }, :as => "shipment_deliver"


Got help from http://www.raysrashmi.com/2014/07/23/rails-routes-constraints . Still, I would like to here about the best practices ....

Arup Rakshit

unread,
Apr 13, 2015, 10:24:02 AM4/13/15
to spree...@googlegroups.com
Hi I have the view :

<div id="<%= "shipment_#{shipment.id}" %>" data-hook="admin_shipment_form">
 
<%= render :partial => "spree/admin/variants/split", :formats => :js %>
 
<fieldset class="no-border-bottom">
   
<legend align="center" class="stock-location" data-hook="stock-location">
     
<span class="shipment-number"><%= shipment.number %></span>
      -
     
<span class="shipment-state"><%= Spree.t("shipment_states.#{shipment.state}") %></span>
     
<%= Spree.t(:package_from) %>
     
<strong class="stock-location-name" data-hook="stock-location-name">'<%= shipment.stock_location.name %>'</strong>
     
<% if shipment.ready? and can? :update, shipment %>
        -
       
<%= link_to Spree.t(:ship), '#', :class => 'ship button fa fa-arrow-right', :data => {'shipment-number' => shipment.number} %>
     
<% elsif shipment.shipped? and can? :update, shipment %>
        -
       
<%= link_to Spree.t(:deliver), shipment_deliver_api_shipment_path(shipment), {method: :put, :remote => true, data: {'shipment-number' => shipment.number}}, {:class => 'deliver button fa fa-arrow-right'} %>
     
<% end %>
   
</legend>
 
</fieldset>

And route :

  put '/api/shipments/:id/deliver', :to => 'spree/api/shipments#deliver', :as => "shipment_deliver_api_shipment"



 But on page load getting error :

Deface: [WARNING] No :original defined for 'orders_head_customer_status', you should change its definition to include:
 
:original => '6e0919be3a695b5c0b4d0564fb50f88607dd4a02'
 
Rendered spree/admin/orders/_shipment.html.erb (194.6ms)
 
Rendered /home/deploy/.rvm/gems/ruby-2.1.2/bundler/gems/spree-080df18614ba/backend/app/views/spree/admin/orders/_form.html.erb (245.9ms)
 
Rendered /home/deploy/.rvm/gems/ruby-2.1.2/bundler/gems/spree-080df18614ba/backend/app/views/spree/admin/orders/edit.html.erb within spree/layouts/admin (671.2ms)
Completed 500 Internal Server Error in 1079ms


ActionView::Template::Error (undefined method `shipment_deliver_api_shipment_path' for #<#<Class:0x00000002c9fac0>:0x00000002c94af8>):
    12:         <%= link_to Spree.t(:ship), '#', :class => 'ship button fa fa-arrow-right', :data => {'shipment-number' => shipment.number} %>
    13:       <% elsif shipment.shipped? and can? :update, shipment %>
    14:         -
    15:         <%= link_to Spree.t(:deliver), shipment_deliver_api_shipment_path(shipment), {method: :put, :remote => true, data: {'shipment-number' => shipment.number}}, {:class => 'deliver button fa fa-arrow-right'} %>
    16:       <% end %>
    17:     </legend>
    18:   </fieldset>
  app/views/spree/admin/orders/_shipment.html.erb:15:in `
_b2c72dca56ef03560050143de6a9a604'


Any idea what's wrong I am doing ?


On Monday, April 13, 2015 at 6:17:49 PM UTC+5:30, Arup Rakshit wrote:

Arup Rakshit

unread,
Apr 14, 2015, 2:58:18 AM4/14/15
to spree...@googlegroups.com
Hello,

Just to confirm that this issue resolved. I got a help from @sevenseacat over #rubyonrails on this. As per this, http://guides.rubyonrails.org/engines.html#routes, I need to rewrite the line as :

<div id="<%= "shipment_#{shipment.id}" %>" data-hook="admin_shipment_form">
 
<%= render :partial => "spree/admin/variants/split", :formats => :js %>
 
<fieldset class="no-border-bottom">
   
<legend align="center" class="stock-location" data-hook="stock-location">
     
<span class="shipment-number"><%= shipment.number %></span>
      -
     
<span class="shipment-state"><%= Spree.t("shipment_states.#{shipment.state}") %></span>
     
<%= Spree.t(:package_from) %>
     
<strong class="stock-location-name" data-hook="stock-location-name">'<%= shipment.stock_location.name %>'</strong>
     
<% if shipment.ready? and can? :update, shipment %>
        -
       
<%= link_to Spree.t(:ship), '#', :class => 'ship button fa fa-arrow-right', :data => {'shipment-number' => shipment.number} %>
     
<% elsif shipment.shipped? and can? :update,
shipment %>
        -
       
<%= link_to Spree.t(:deliver), main_app.shipment_deliver_path(shipment), {method: :put, :remote => true, data: {'shipment-number' => shipment.number}, :class => 'deliver button fa fa-arrow-right'} %>
     
<% end %>
   
</legend>
 
</fieldset>


Look the part `main_app.shipment_deliver_path(shipment)`. And this issue is resolved.

On Monday, April 13, 2015 at 6:17:49 PM UTC+5:30, Arup Rakshit wrote:
Reply all
Reply to author
Forward
0 new messages