我正在开发一个基于Ruby on Rails构建的Spree电子商务商店,并且想要一个自定义操作,用户可以直接从结帐页面标记订单,而无需通过交付等。我已经覆盖了所有的结帐步骤但无法通过将订单发送到订单控制器中的自定义操作来获取“结帐”按钮来完成订单。
我想我已经勾选了所有方框:在routes.rb中创建了一个补丁操作并检查了rake路由以确保路由存在。但它仍然告诉我没有路线。
在我提交任何内容之前,购物车页面甚至不会加载,并出现以下错误。我整天都在努力解决这个问题,所以任何想法都会很棒......
错误:
No route matches {:action=>"complete", :controller=>"spree/orders", :method=>:patch}
routes.rb中:
resources :orders do
member do
patch 'complete', to: 'orders#complete'
end
end
耙路线:
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 %>
我还没有创建控制器,但它会是:
def complete
# mark order as complete
# redirect to confirmation page
end
非常感谢任何帮助。感谢
编辑:这是更新后的视图(app / views / orders / edit.html.erb):
<% @body_id = 'cart' %>
<div data-hook="cart_container">
<h1><%= Spree.t(:shopping_cart) %></h1>
<% if @order.line_items.empty? %>
<div data-hook="empty_cart">
<div class="alert alert-info"><%= Spree.t(:your_cart_is_empty) %></div>
<p><%= link_to Spree.t(:continue_shopping), products_path, class: 'btn btn-default' %></p>
</div>
<% else %>
<div data-hook="outside_cart_form">
<%= form_for @order, url: update_cart_path, html: { id: 'update-cart' } do |order_form| %>
<div data-hook="inside_cart_form">
<div data-hook="cart_items" class="table-responsive">
<%= render partial: 'form', locals: { order_form: order_form } %>
</div>
</div>
<% end %>
</div>
<div id="empty-cart" class="col-md-6" data-hook>
<%= form_tag empty_cart_path, method: :put do %>
<p id="clear_cart_link" data-hook>
<%= submit_tag Spree.t(:empty_cart), class: 'btn btn-default' %>
<%= Spree.t(:or) %>
<%= link_to Spree.t(:continue_shopping), products_path, class: 'continue' %>
</p>
<% end %>
</div>
<div id="complete-order">
complete order here - submit to custom controller
<%= @order.id %>
<%= form_for @order, url: complete_order_path(@order) do |f| %>
<% f.submit %>
<% end %>
</div>
<% end %>
</div>
这是整个控制器:
module Spree
class OrdersController < Spree::StoreController
before_action :check_authorization
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
helper 'spree/products', 'spree/orders'
respond_to :html
before_action :assign_order_with_lock, only: :update
skip_before_action :verify_authenticity_token, only: [:populate]
def show
@order = Order.includes(line_items: [variant: [:option_values, :images, :product]], bill_address: :state, ship_address: :state).find_by_number!(params[:id])
end
def complete
@order = current_order
end
def update
if @order.contents.update_cart(order_params)
respond_with(@order) do |format|
format.html do
if params.has_key?(:checkout)
@order.next if @order.cart?
redirect_to checkout_state_path(@order.checkout_steps.first)
else
redirect_to cart_path
end
end
end
else
respond_with(@order)
end
end
# Shows the current incomplete order from the session
def edit
@order = current_order || Order.incomplete.
includes(line_items: [variant: [:images, :option_values, :product]]).
find_or_initialize_by(guest_token: cookies.signed[:guest_token])
associate_user
end
# Adds a new item to the order (creating a new order if none already exists)
def populate
order = current_order(create_order_if_necessary: true)
variant = Spree::Variant.find(params[:variant_id])
quantity = params[:quantity].to_i
options = params[:options] || {}
# 2,147,483,647 is crazy. See issue #2695.
if quantity.between?(1, 2_147_483_647)
begin
order.contents.add(variant, quantity, options)
rescue ActiveRecord::RecordInvalid => e
error = e.record.errors.full_messages.join(", ")
end
else
error = Spree.t(:please_enter_reasonable_quantity)
end
if error
flash[:error] = error
redirect_back_or_default(spree.root_path)
else
respond_with(order) do |format|
format.html { redirect_to cart_path }
end
end
end
def empty
if @order = current_order
@order.empty!
end
redirect_to spree.cart_path
end
def accurate_title
if @order && @order.completed?
Spree.t(:order_number, :number => @order.number)
else
Spree.t(:shopping_cart)
end
end
def check_authorization
order = Spree::Order.find_by_number(params[:id]) || current_order
if order
authorize! :edit, order, cookies.signed[:guest_token]
else
authorize! :create, Spree::Order
end
end
private
def order_params
if params[:order]
params[:order].permit(*permitted_order_attributes)
else
{}
end
end
def assign_order_with_lock
@order = current_order(lock: true)
unless @order
flash[:error] = Spree.t(:order_not_found)
redirect_to root_path and return
end
end
end
end
修改
自从我发布这个问题以来,您确实需要以特殊方式声明您的路线,尽管 rake路线显示它们是正确的。
在routes.rb中,添加:
Spree::Core::Engine.routes.draw do
# add your custom routes here, e.g.
get '/terms-and-conditions' => 'home#terms', as: :terms
end
这将允许您使用&lt;%= link_to(“Terms”,terms_path)%&gt;帮手。
有关详细信息,请参阅Adding Routes to Rails' Spree E-Commerce。我希望文档在这方面做得更好,因为据我所知,除了SO之外,它已被提及。
答案 0 :(得分:2)
这是因为您没有将对象传递给表单。所以路由中没有id
参数,路由器无法匹配。
您的路线定义为member
action,这意味着它需要id
参数。你正在传递一个符号。
<%= form_for :order <-- problem
线索在错误消息中:
No route matches {:action=>"complete", :controller=>"spree/orders", :method=>:patch}
注意错误消息中哈希中没有id
参数?
要解决此问题,请向表单提供一个对象。例如:
<%= form_for @order, url: complete_order_path(@order) do |f| %>
在控制器中设置@order
实例变量。
在旁注中,您可以像这样定义路线:
resources :orders do
member do
patch :complete
end
# or, since it's only one route...
patch :complete, on: :member
end
请注意,您可以使用符号,而不必指定控制器,因为它是从资源名称推断出来的。
最后,您不需要告诉表单该方法应该是补丁。 Rails从传入的对象中推断出这一点,在本例中为@order
。如果它是
新方法,方法为POST
,否则为PATCH
。