我正在尝试使用我在观看Ryan Bates的祖先视频时发现的祖先宝石,所以我设置了我的东西,像这样:
迁移:class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :comment
t.string :author
t.integer :post_id
t.string :ancestry
t.index :ancestry
t.timestamps
end
end
end
模型:
class Comment < ActiveRecord::Base
belongs_to :post
has_ancestry
validates :comment, presence: true, length: {minimum: 15}
validates :author, presence: true
end
**控制器新动作:**
def new
post = Post.find_by(id: params[:post_id])
post.comments.new(:ancenstry => params[:parent_id])
end
所以我想我已经正确地设置了一切。但是当我运行以下测试时:
it "should create a nested comment for a post" do
posted_comment = FactoryGirl.create(:comment, post_id: @post.id)
post :create, :post_id => @post.id, comment: {author: @comment.author, comment: @comment.comment, parent_id: @comment.id}
json = JSON.parse(response.body).to_json
binding.pry
parse_json(json, 'comment').to_json.should have_json_path('id')
end
并检查绑定后的json:
{
"comment":{
"id":9,
"post_id":6,
"author":"Adam",
"comment":"Some Really long Valid Comment length of Something.",
"ancestry":null
}
}
祖先部分为空。我甚至尝试过将parent_id
改为ancestry
,但这也无济于事。有人知道我做错了什么吗?或者有什么想法吗?
post.comments.new(:ancenstry => params[:parent_id])
您的散列键拼写错误。