2013-03-14 2 views
0

내가 레일에 루비에 해시에 새 키와 값을 추가에 문제가에 루비에 해시합니다. 방법은 두 가지 디버그 인쇄로 다음과 같습니다 단순히 나중에 올바른 ID와 공급자에 액세스하려면, 값으로 인덱스 + 1으로 제공 키를 추가해야합니다.추가 값은 레일

search_result.each_with_index do |articles, index| 
    puts "merge-articles: #{articles}" 
    articles.each{ |article| article[:provider] = index + 1;} 
    puts "merge-articles(later): #{articles}" 
end           

내가 내 마음에 아주 좋은 보는 풋에서 그 출력을 얻을 :

Failure/Error: HomeController.merge(@no_same_items_merge).each do |item| 
TypeError: 
    can't convert Symbol into Integer 
# ./app/controllers/home_controller.rb:41:in `[]=' 
# ./app/controllers/home_controller.rb:41:in `block (2 levels) in merge' 
# ./app/controllers/home_controller.rb:41:in `each' 
# ./app/controllers/home_controller.rb:41:in `block in merge' 
# ./app/controllers/home_controller.rb:39:in `each' 
# ./app/controllers/home_controller.rb:39:in `each_with_index' 
# ./app/controllers/home_controller.rb:39:in `merge' 
# ./spec/controllers/home_controller_spec.rb:104:in `block (5 levels) in <top (required)>' 

: 만 테스트는 키가 존재하는 경우 사양을

merge-articles: [{:ean=>"9780234474278", :author=>"Dan Brown", :name=>"The Da Vinci Code", :price=>19.65, :image=>"www.google.de/image.png"}] 
merge-articles(later): [{:ean=>"9780234474278", :author=>"Dan Brown", :name=>"The Da Vinci Code", :price=>19.65, :image=>"www.google.de/image.png", :provider=>2}] 

하면이 오류를 얻을 수 편집은 다음 RSpec에 테스트는 다음과 같습니다

it "should return an array of right formatted hashes" do 
     HomeController.merge(@no_same_items_merge).each do |item| 
     item.should have_key(:name) 
     item.should have_key(:ean) 
     item.should have_key(:author) 
     item.should have_key(:description) 
     item.should have_key(:url) 
     item.should have_key(:prices) 
     item.should have_key(:images) 
     end 
    end 
,536,

도움 주셔서 감사합니다!

+0

당신은 RSpec에 시험에 붙여 넣을 수 있습니까? 거기에 문제가 될 수 있으며 코드에 없습니다. –

+4

그 오류가 강하게 사양에'article'는'Array' 아닌'Hash' 것을 의미한다; spec에서'article'을 정의하는 방법을 확인하십시오. – bloopletech

+0

@bloopletech는 article'이 Hash''의'Array'이다', 권리입니다. – shweta

답변

2

articleHash이 아닌 Array으로 설정하십시오.