2014-06-17 3 views
1

내가 옛날 Ryan Bates Ancestry video를보고에 의해 발견 그래서 내가 그렇게처럼 내 내 물건을 설정 ancestry gem 사용하는 것을 시도하고있다 :확실 레일의 조상 보석은 어떻게 작동하는지

마이그레이션 :

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 
    } 
} 

조상 부분은 null입니다. 나는 심지어 parent_idancestry으로 변경하려고 시도했지만 그 역시 도움이되지 않습니다. 어떤 사람이 내가 뭘 잘못하고 있는지 알아? 또는 어떤 아이디어가 있습니까?

+0

'ancestry'의 철자가 잘못되었습니다. 이것은 오로지 오타에있는 오타입니까? 아니면 이것이 당신의 문제를 일으킬 수 있습니까? –

답변

0

post.comments.new(:ancenstry => params[:parent_id])

은 당신의 해시 키는 철자가 잘못되었습니다.