gpt4 book ai didi

ruby-on-rails - 在 ruby​​ on rails 中销毁客户/订单

转载 作者:数据小太阳 更新时间:2023-10-29 09:01:36 26 4
gpt4 key购买 nike

各位程序员大家好,我一直在研究订单管理系统。我终于解决了我所有的错误,除了一个我无法改正的错误。删除客户或订单后,我会收到一条错误消息,提示“路由错误”。

Routing Error No route matches [POST] "/customers/2"

Rails.root: /Users/cecil/Desktop/order_management_systeem

Application Trace | Framework Trace | Full Trace Routes

这是我的路线

           Prefix Verb   URI Pattern                                       Controller#Action
customer_orders GET /customers/:customer_id/orders(.:format) orders#index
POST /customers/:customer_id/orders(.:format) orders#create
new_customer_order GET /customers/:customer_id/orders/new(.:format) orders#new
edit_customer_order GET /customers/:customer_id/orders/:id/edit(.:format) orders#edit
customer_order GET /customers/:customer_id/orders/:id(.:format) orders#show
PATCH /customers/:customer_id/orders/:id(.:format) orders#update
PUT /customers/:customer_id/orders/:id(.:format) orders#update
DELETE /customers/:customer_id/orders/:id(.:format) orders#destroy
customers GET /customers(.:format) customers#index
POST /customers(.:format) customers#create
new_customer GET /customers/new(.:format) customers#new
edit_customer GET /customers/:id/edit(.:format) customers#edit
customer GET /customers/:id(.:format) customers#show
PATCH /customers/:id(.:format) customers#update
PUT /customers/:id(.:format) customers#update
DELETE /customers/:id(.:format) customers#destroy
GET /:controller/:action/:id/:customer_id(.:format) :controller#:action

这是我的erb代码

<%= link_to("Delete", customer_path(@customer), method: :delete, confirm: "Are you sure?", :class => 'action delete') %>

销毁 Controller

 def destroy
customer = Customer.find(params[:id]).destroy
flash[:notice] = "Subject '#{customer.first_name}' destroyed successfully"
redirect_to(:action => 'index')
end

最佳答案

销毁操作应该类似于以下几行:

def destroy
@customer = Customer.find(params[:id]
@customer.destroy
redirect_to(
customers_path,
notice: 'Customer successfully deleted'
)
end

同时编辑链接:

<%= link_to("Delete", @customer, method: :delete, data: { confirm: "Are you sure?" }) %>

你的问题是,你已经定义了变量customer,但是在你的url_helper (customer_path(@customer)).

另一件事(更重要)是,您实际上为 客户 分配了一个值,这是从数据库中删除对象的结果:

customer = Customer.find(params[:id]).destroy

永远不要这样做。

要么做

Customer.find(params[:id]).destroy

@customer = Customer.find(params[:id])
@customer.destroy

关于ruby-on-rails - 在 ruby​​ on rails 中销毁客户/订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34085302/

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