在轨道上销毁ruby中的客户/订单

时间:2015-12-04 09:48:03

标签: ruby-on-rails ruby rest http ruby-on-rails-4

Hello其他程序员,我一直在研究订单管理系统。我终于解决了我的所有错误,除了一个我不能出去。一旦我删除了客户或订单,我会收到一条错误消息,提示"路由错误"。

  

路由错误没有路由匹配[POST]" / customers / 2"

     

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

     

应用程序跟踪|框架跟踪|完整跟踪路径

这是我的路线

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

销毁控制器

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

1 个答案:

答案 0 :(得分:2)

动作破坏应该看起来像以下几行:

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)中使用customer_path(@customer)

另一件事(更重要的)是,你实际上分配了一个customer一个值,它是从数据库中删除一个对象的结果

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

永远不要这样做。

要么

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

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