gpt4 book ai didi

ruby-on-rails - Ruby on Rails - form_for 的自定义 PATCH 操作

转载 作者:行者123 更新时间:2023-12-04 06:06:22 25 4
gpt4 key购买 nike

我正在开发一个基于 Ruby on Rails 的 Spree 电子商务商店,我想要一个自定义操作,用户可以在其中直接从结帐页面将他们的订单标记为完整,而无需通过交付等。我已经覆盖了所有结帐步骤,但无法通过将订单发送到订单 Controller 中的自定义操作来获得“结帐”按钮来完成订单。

我想我已经勾选了所有方框:在 routes.rb 中创建了一个补丁操作并检查了 rake 路由以确保该路由存在。但它仍然告诉我没有路线。

在我提交任何内容之前,购物车页面甚至都不会加载,并出现以下错误。我花了一整天的时间试图解决这个问题,所以任何想法都会很棒....

错误:

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

路线.rb:

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

rake 路线:

        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 %>

我还没有创建 Controller ,但它会是:

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

非常感谢任何帮助。谢谢

编辑:这是更新后的 View (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>

这是整个 Controller :

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

这将允许您使用 <%= link_to("Terms", terms_path) %> 助手。

参见 Adding Routes to Rails' Spree E-Commerce更多细节。我希望文档在这方面做得更好,因为据我所知,除了 SO 之外没有提到它。

最佳答案

发生这种情况是因为您没有将对象传递给表单。所以路由中没有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| %>

在 Controller 中设置@order实例变量。

附带说明一下,您可以像这样定义您的路线:

resources :orders do
member do
patch :complete
end
# or, since it's only one route...
patch :complete, on: :member
end

请注意,您可以使用符号,并且不必指定 Controller ,因为它是从资源名称中推断出来的。

最后,您不需要告诉表单该方法应该是补丁。 Rails 从传入的对象中推断出这一点,在本例中为 @order。如果它是new,方法将为POST,否则为PATCH

关于ruby-on-rails - Ruby on Rails - form_for 的自定义 PATCH 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com