我正在爲此行收到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
你有沒有得到這個解決? –