【发布时间】: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