为什么我的链接与正确的操作无关?

时间:2014-04-02 15:51:28

标签: ruby-on-rails

我有以下链接:

<%= link_to 'Delete', store_product_path(@store, @product), :method => 'delete', :confirm => 'This cannot be undone. Are you sure?', :class => 'btn'  %>

这就是编译HTML的样子:

<a href="/stores/237/products/193-no-image" class="btn" data-confirm="This cannot be undone. Are you sure?" data-method="delete" rel="nofollow">Delete</a>

它应该做的就是删除该产品,但它现在与products#show操作相关联,而不是products#destroy操作应该关联。

当我在rake routes运行产品时返回的内容:

                      products GET    /products(.:format)                                                   products#index
                               POST   /products(.:format)                                                   products#create
                   new_product GET    /products/new(.:format)                                               products#new
                  edit_product GET    /products/:id/edit(.:format)                                          products#edit
                       product GET    /products/:id(.:format)                                               products#show
                               PUT    /products/:id(.:format)                                               products#update
                               DELETE /products/:id(.:format)                                               products#destroy

所以,我不确定该链接有什么问题。是否有更冗长的方式来编写可能对我有用的链接?感谢

1 个答案:

答案 0 :(得分:4)

目前,您在“商店”中没有嵌套资源“产品”。根据耙路线, 您已将产品的路线定义为

resources :products

所以,你的链接应该是

<%= link_to 'Delete', product_path(@product), :method => 'delete', :confirm => 'This cannot be undone. Are you sure?', :class => 'btn'  %>

假设您有一个名为stores的资源,那么在这种情况下,为了实现销毁行为{@ 1}}之类的路线:

路线应定义为:

/stores/237/products/193-no-image

您当前的链接可以正常工作。 只要路线按照上面的建议定义,就无需更改任何内容。