2013-07-16 25 views
0

试图创建类似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. 

看起来我不能嵌入在发表评论,并subcomments。考虑mongoid_acts_as_tree或创建另一个模型。

+0

在你的'params'中你有':parent_comment_id'而不是':parent_comment',但是在你的控制器方法中你*正在使用':parent_comment',它不存在。我不知道第一个错误。我没有看到你的代码中的任何地方。 – Mischa

回答

0

似乎嵌入式不是正确的选择。

SOLUTION

class Comment 
    include Mongoid::Document 

    field :text, type: String 
    field :created, type: Time, default: Time.now 

    belongs_to :user 
    belongs_to :post 
    belongs_to :parent_comment, class_name: "Comment", inverse_of: :child_comments 
    has_many :child_comments, class_name: "Comment", inverse_of: :parent_comment 
end 
0

这发生在我身上时,我创建内部嵌入1-1关系的1-N嵌入式关系。

level4.business_function = BusinessFunction.new 
    level4.business_function.function = row[4].to_s 
    level4.business_function.category_tree = (row[0].to_s+"/"+row[1].to_s+"/"+row[2].to_s+"/"+row[3].to_s) 
    level4.business_function.unit = row[5].to_s 
    level4.business_function.eco = row[6].to_s 
    level4.business_function.pto = row[7].to_s 
    level4.business_function.save! 

这是1-1,然后它解决了,但是当我重复1-n时,它给出了你所得到的错误。所以我创建了这样的文件,它的工作原理。

var.each_with_index do |v, index| 
    level4.business_function.variations.create(variation: v, base_price: base_prices[index]) 
end 
相关问题