0
我想爲我的網站上的用戶創建一種方法來刪除評論。我已經建立了我的看法是這樣Rails找不到'ID'= 27的部件[WHERE「parts」。「active」=?] PartsController中的RecordNotFound#Destroy
<li><%= comment.content%> by: <%= comment.user.first_name %> </li>
<% if logged_in? %>
<%=link_to 'delete', part_comment_path(@part, comment), data: {confirm: "Are you sure you want to delete this comment?"}, method: 'DELETE'%>
<%end%>
當我點擊刪除鏈接出現錯誤「無法找到‘身份證’= 27 [其中部分‘零件’,」激活「=?]」它來自零件控制器。目前我的評論控制器是這樣設置的。
class CommentsController < ApplicationController
def create
part = Part.find(params[:part_id])
@comment = part.comments.create(comment_params.merge(user: current_user))
respond_to do |format|
format.html {redirect_to part}
format.js{}
end
end
def destroy
@part = Part.find(params[:part_id])
@comment = @part.comments.find(params[:id])
@comment.destroy
format.html {redirect_to part}
format.js{}
end
private
def comment_params
params.require(:comment).permit(:content)
end
end
和我的部件控制器設置這樣
class PartsController < ApplicationController
before_filter :authorize, :except => [:index, :show]
def index
@parts = Part.all
@categories = Category.all
@parts = @parts.search(params[:search]) if params[:search].present?
end
def new
@part = Part.new
end
def show
@part = Part.find(params[:id])
end
def create
@part = Part.new(part_params)
if @part.save
redirect_to part_path(@part)
end
end
def edit
@part = Part.find(params[:id])
end
def update
@part = Part.find(params[:id])
if @part.update_attributes(part_params)
redirect_to @part
end
end
def destroy
@part = Part.find(params[:id])
@part.destroy
redirect_to parts_path
end
private
def part_params
params.require(:part).permit(:description, :name, :price, :active, :avatar, :discount, :category_id)
end
end
我徵求意見路線嵌套
resources :parts do
resources :comments, only: [:create, :destroy]
end
我的模型佈置成一個評論belongs_to的零件和零件has_many評論。感謝您對這一個的幫助
試試這個redirect_to @part在你的銷燬方法 – uzaif
你可以運行'rake routes'並粘貼結果嗎?並且還可以爲請求提供服務器日誌和堆棧跟蹤? –
嘗試任何其他評論,你可以刪除並告訴我 –