尝试创建类似 reddit 的东西,每条评论都有帖子,每条评论都可能有子评论。为 ember.js 创建 json api。
我的创建方法:
def create
@comment = Comment.new
@comment.text = params[:comment][:text]
@comment.user = current_user
@comment.created = Time.now
if params[:comment][:parent_comment]
@parent = Comment.find(params[:comment][:parent_comment])
@comment.parent_comment = @parent
end
@comment.post = Post.find(params[:comment][:post_id])
@comment.save
end
模型:
class Comment
include Mongoid::Document
field :text, type: String
field :created, type: Time, default: Time.now
belongs_to :user
embedded_in :post
recursively_embeds_many
end
错误:
Started POST "/api/v1/comments" for 127.0.0.1 at 2013-07-16 16:34:49 +0300
Processing by Api::V1::CommentsController#create as JSON
Parameters: {"comment"=>{"text"=>"123", "created"=>nil, "user_id"=>"51e51f301c3167fb35000001", "post_id"=>"51e549961c3167ee53000002", "parent_comment_id"=>nil}}
MOPED: 127.0.0.1:27017 QUERY database=ember_js_development collection=users selector={"$query"=>{"_id"=>"51e51f301c3167fb35000001"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.0000ms)
MOPED: 127.0.0.1:27017 QUERY database=ember_js_development collection=posts selector={"_id"=>"51e549961c3167ee53000002"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.0000ms)
Completed 500 Internal Server Error in 10ms
NoMethodError (undefined method `first' for #<Comment:0x0000000ba45c08>):
app/controllers/api/v1/comments_controller.rb:24:in `create'
Rendered C:/Programming/RailsInstaller/Ruby2.0.0-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/Programming/RailsInstaller/Ruby2.0.0-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered C:/Programming/RailsInstaller/Ruby2.0.0-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered C:/Programming/RailsInstaller/Ruby2.0.0-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (44.0ms)
Github:https ://github.com/mongoid/mongoid/issues/3162
更新 此方法有效:
def create
@comment = Comment.new
@comment.text = params[:comment][:text]
@comment.user = current_user
@comment.created = Time.now
if params[:comment][:parent_comment]
@parent = Comment.find(params[:comment][:parent_comment])
@comment.parent_comment = @parent
end
@comment.post = Post.find(params[:comment][:post_id])
@comment.save
render json: @comment
end
但 似乎 parent_comment 没有保存。尝试使用控制台玩,您可以设置父母和孩子,但它不会保存到数据库中。
UPDATE 修复了未定义的第一个问题,新的错误
Mongoid::Errors::InvalidPath:
Problem:
Having a root path assigned for Comment is invalid.
Summary:
Mongoid has two different path objects for determining the location of a document in the database, Root and Embedded. This error is raised when an embedded document somehow gets a root path assigned.
看起来我无法在帖子和子评论中嵌入评论。考虑 mongoid_acts_as_tree 或创建另一个模型。