2016-03-28 19 views
0

私はAmazonの広告宣伝APIを使用しています。大部分の検索結果を検索してレンダリングしていますが、一部のフィールドでAPI呼び出し結果の値がnilであればクラッシュするようです。API呼び出し=未定義メソッド `[] 'for nil:NilClass

undefined method `[]' for nil:NilClass 

以下である:

が、私は三項演算子を使って、それを克服しようとした、しかし、これはまだ、次のエラーメッセージ内のデータ要素の結果が欠落してAPI呼び出しが動作していないよう私のコントローラから該当するコード:

オリジナル試み:使用中の

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item['ItemAttributes']['Title'] 
    product.image = item['MediumImage']['URL'] 
    product.ASIN = item['ASIN'] 
    product.URL = item['DetailPageURL'] 
    product.Feature = item['ItemAttributes']['Feature'] 
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'] 
    @products << product 
end 

三項演算子:

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item['ItemAttributes']['Title'] ? item['ItemAttributes']['Title'] : nil 
    product.image = item['MediumImage']['URL'] ? item['MediumImage']['URL'] : nil 
    product.ASIN = item['ASIN'] ? item['ASIN'] : nil 
    product.URL = item['DetailPageURL'] ? item['DetailPageURL'] : nil 
    product.Feature = item['ItemAttributes']['Feature'] ? item['ItemAttributes']['Feature'] : nil 
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'] ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil 

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil 
     @products << product 
    end 
end 

答えて

0

hash ['key']を使用する必要があります。三項演算でテストします。

だからではなく、次のあなたのコード、:

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item['ItemAttributes']['Title'] ? item['ItemAttributes']['Title'] : nil 
    product.image = item['MediumImage']['URL'] ? item['MediumImage']['URL'] : nil 
    product.ASIN = item['ASIN'] ? item['ASIN'] : nil 
    product.URL = item['DetailPageURL'] ? item['DetailPageURL'] : nil 
    product.Feature = item['ItemAttributes']['Feature'] ? item['ItemAttributes']['Feature'] : nil 
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'] ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil 

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil 
     @products << product 
    end 
end 

次のようにする必要があります:

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item['ItemAttributes']['Title'].present? ? item['ItemAttributes']['Title'] : nil 
    product.image = item['MediumImage']['URL'].present? ? item['MediumImage']['URL'] : nil 
    product.ASIN = item['ASIN'].present? ? item['ASIN'] : nil 
    product.URL = item['DetailPageURL'].present? ? item['DetailPageURL'] : nil 
    product.Feature = item['ItemAttributes']['Feature'].present? ? item['ItemAttributes']['Feature'] : nil 
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'].present? ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil 

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil 
     @products << product 
    end 
end 

をしかし、私は非常にあなたがここで達成しようとしているのか理解していない:

if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil 
    @products << product 
end 

product@productsアレイに追加しようとしていますか?例えば、Title,image,ASIN,URL,FeatureまたはPriceのいずれかが存在する場合、そうであれば、コードは機能しません。それがいずれかまたはitemのすべての属性はオプションであることも可能であるとして、あなたの代わりに三項演算で、以下のアプローチを使用することができ :

if product.Title.present? || product.image.present? || product.ASIN.present? || product.URL.present? || product.Feature.present? || product.Price.present? 
    @products << product 
end 

UPDATE:あなたは次のようにそれを更新する必要があり

@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item| 
    product = OpenStruct.new 
    product.Title = item.try(:[], 'ItemAttributes').try(:[], 'Title') 
    product.image = item.try(:[], 'MediumImage').try(:[], 'URL') 
    product.ASIN = item.try(:[], 'ASIN') 
    product.URL = item.try(:[], 'DetailPageURL') 
    product.Feature = item.try(:[], 'ItemAttributes').try(:[], 'Feature') 
    product.Price = item.try(:[], 'OfferSummary').try(:[], 'LowestNewPrice').try(:[], 'FormattedPrice') 

    if product.Title.present? || product.image.present? || product.ASIN.present? || product.URL.present? || product.Feature.present? || product.Price.present? 
    @products << product 
    end 
end 
+0

こんにちは@ダーラム、私は三元演算子が実際に最良のアプローチであるかどうかはわかりません。私はあなたのソリューションを試して、.presentを追加しましたか?しかし、それはまだいくつかの基準がNILであるXMLデータセットでクラッシュしています。他のアプローチを念頭に置いてですか? –

+0

スタックトレースを使用して、障害が発生している場所を確認します。 – Dharam

+0

product.imageでエラーが発生したことを示すエラーメッセージが表示されます。だから私は、すべてのアイテムがイメージを持っているわけではない、または中規模のイメージを持っていると仮定します –

関連する問題