【问题标题】:trying to add nested comments with closure_tree尝试使用closure_tree 添加嵌套注释
【发布时间】:2014-12-26 21:47:10
【问题描述】:

我正在尝试使用closure_tree gem 添加嵌套的 cmets - 以及此站点指南 http://www.sitepoint.com/nested-comments-rails/

很遗憾,我在回复评论后不断出现此错误: ActiveRecord::StatementInvalid 在 CommentsController#create SQLite3::SQLException: 在 "DESC1" 附近: 语法错误: SELECT "cmets".* FROM "cmets" WHERE "cmets"."parent_id" = ? ORDER BY created_at DESC1

它指向我的“创建”方法中的第 18 行 - 这是if @comment.save 行,这叫我疯了,但我没有看到语法错误

任何帮助将不胜感激,谢谢。

这是我的评论控制器:

class CommentsController < ApplicationController
  def index
    @comments = Comment.hash_tree
  end

  def new
    @comment = Comment.new(parent_id: params[:parent_id])
  end

  def create
    if params[:comment][:parent_id].to_i > 0
      parent = Comment.find_by_id(params[:comment].delete(:parent_id))
      @comment = parent.children.build(comment_params)
    else
      @comment = Comment.new(comment_params)
    end

    if @comment.save
      flash[:success] = 'Your comment was successfully added!'
      redirect_to root_url
    else
      render 'new'
    end
  end

  private
  def comment_params
     params.require(:comment).permit(:title, :body, :author)
  end
end

这是我的 cmets 模型,大部分是空的

class Comment < ActiveRecord::Base
    acts_as_tree order: 'created_at DESC1'
end

【问题讨论】:

  • 请发表您的评论模型
  • 刚刚贴在上面。它大多是空的。感谢您的帮助。

标签: ruby-on-rails ruby nested-attributes


【解决方案1】:

您的评论模型中有错字。将“DESC1”更改为“DESC”

【讨论】:

  • 太尴尬了……谢谢。
猜你喜欢
  • 2013-06-21
  • 2013-08-11
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多