Rails找不到部分'id'= 27 [WHERE“parts”。“active”=?] PartController中的RecordNotFound #Destroy

时间:2016-04-12 02:57:59

标签: ruby-on-rails comments

我正在尝试为我网站上的用户创建一种删除评论的方法。我已经设置了这样的视图

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

当我点击删除链接时,我收到错误“无法找到带有'id'的零件= 27 [WHERE”parts“。”active“=?]”来自零件控制器。目前我的评论控制器设置如下。

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

我的模型的排列方式使得评论属于一个零件和一个零件has_many评论。感谢您对此的帮助

1 个答案:

答案 0 :(得分:1)

我认为删除方法不起作用。请尝试使用此&lt;%= link_to&#39;删除&#39;,part_comment_path(@part,comment),数据:{确认:&#34;您确定要吗?删除此评论?&#34;},方法:: delete&gt;。并检查是否有其他评论