2017-04-07 28 views
0

我正在爲此行收到NoMethodError <%= link_to「Delete」,delete_product_path(p),:confirm =>「Are you sure?」,:method =>:delete%>或<%=的link_to 「刪除」,delete_product_path(p):確認=> 「你確定?」:方法=>:刪除%> ..我使用的資源爲我的路線..產品中的NoMethodError#刪除方法中的索引

問題:這是爲什麼?

注:我也嘗試過<%=的link_to '刪除',delete_product_path(P)之類的視頻中說,這不是對我的工作

查看

<html> 
<head> 
    <title>MY STORE!</title> 
</head> 
<body> 
    <h1><align="center"> WELCOME TO MY STORE</h1> 
    <%= link_to 'Add Product', new_product_path %> 
    <table border = "1" width="100%"> 
     <tr> 
      <td>ID</td> 
      <td>Name</td> 
      <td>Image</td> 

      <td>Size</td> 
      <td>Price</td> 
      <td>Created At</td> 
      <td>Updated At</td> 
      <td>Action</td> 
     </tr> 
     <% @product.each do |p| %> 

     <tr> 
      <td><%= p.id %></td> 
      <td><%= p.name %></td> 

      <td><%= p.size %></td> 
      <td><%= p.price %></td> 
      <td><%= p.created_at.strftime("%B, %d, %Y") %></td> 
      <td><%= p.updated_at.strftime("%B, %d, %Y") %></td> 
      <td> 
      <%= link_to 'View', product_path(p) %> 
      <%= link_to 'Edit', edit_product_path(p) %> 
      <%= link_to 'Delete', delete_product_path(p), :confirm => "Are you sure?", :method => :delete %> 
      </td> 
     </tr> 
     <% end %> 

    </table> 
</body> 
</html> 

路線

Rails.application.routes.draw do 
resources :products 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
end 

控制器

class ProductsController < ApplicationController 
    def index 
    @product = Product.all.order('created_at DESC') 

    end 
    def show 
    @post = Product.find(params[:id]) 
    end 
    def new 
    @product = Product.new 
    end 
    def create 
    @post = Product.new(post_params) 
    if @post.save 
    redirect_to (products_path) 
    else 
    redirect_to (new_product_path) 
    end 
    end 
    def edit 
    @product = Product.find(params[:id]) 

    end 
    def update 
    @product = Product.find(params[:id]) 
    if @product.update_attributes(post_params) 
    redirect_to (products_path) 
    else 
    redirect_to (products_path) 
    end 
    end 
    def delete 
    @product = Product.find(params[:id]) 
    end 
    def destroy 
    @product = Product.find(params[:id]) 
    @product.destroy 
    redirect_to(products_path) 

末 私人 高清post_params params.require(:產品).permit(:名稱,:尺寸:價格) 結束

end 
+0

你有沒有得到這個解決? –

回答

1

你想要的是

<%= link_to 'Delete', p, method: :delete, data: { confirm: 'Are you sure?' } %> 

該路線是

​​

因此,指定的路徑不是delete_product_path。它是帶有id的產品路徑和使用方法delete來銷燬它的產品路徑。

從您的控制器上的刪除方法,並設置這樣

# DELETE /products/1 
    # DELETE /products/1.json 
    def destroy 
    @product = Product.find(params[:id]) 
    @product.destroy 
    respond_to do |format| 
     format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

您銷燬方法,它看起來對被破壞的方法。你永遠不會調用「.destroy」這是實際刪除記錄的操作。你的方法所做的是根據id找到它,所以這就是它顯示的原因。

+0

那麼正確的語法是什麼? – Angel

+0

這條線沒有工作嗎? –

+0

它沒有,它只是顯示產品的信息(如顯示方法) – Angel

相關問題