祖先字符串未被定义

时间:2012-09-14 15:43:10

标签: ruby-on-rails ruby

基本上我有一个可以添加注释的类别,此类别显示了任务列表。当您添加评论时,您可以回复所述评论,当您这样做并将鼠标悬停在回复链接上时,您会看到类似的内容:

http://localhost:3000/categories/2/category_comments/new?parent=6

然后,我们将该ID传递给回复论坛,然后将其分配给数据库中的祖先字符串以“嵌套”回复。问题是,父ID没有传递给表单。表单的隐藏字段为空白。为什么?我们可以在下面的代码中走这个id应该采用的路径。

categories_controller

  def show 
    @category = Category.find(params[:id])
    @category_comment = @category.category_comments.build
  end

这会在类别页面上显示评论,并将您回复的评论的parent_id传递给表单。

点击回复后,我们会触发下面显示的category_comments#new#create方法。

category_comments_controller

  def new
     @category = Category.find(params[:category_id])
     @category_comment = @category.category_comments.build(:parent_id => params[:parent_id])
  end

  def create
    @category = Category.find(params[:category_id])
    @category_comment = @category.category_comments.create(params[:category_comment].merge(:user_id => current_user.id))
    if @category_comment.save
      redirect_to project_category_path(@category.project, @category), :flash => {:success => 'Created comment'}
    else
      redirect_to :back, :flash => {:error => 'Could not create comment'}
    end
  end

更新

这不再是表单问题,它是一个控制器问题,处理将parent_id传递给表单。

3 个答案:

答案 0 :(得分:0)

您的模型中是否定义了has_ancestry?我认为没有它会有一个有效的解释,这不起作用。

答案 1 :(得分:0)

试试这个:

<%= link_to 'Reply', new_category_category_comment_path(@category.id, :parent_id => category_comment.id)%>

答案 2 :(得分:0)

一些如何神奇地修复自我。我不确定发生了什么或发生了什么,但它现在神奇地起作用&gt;。&gt;