扶手:如何遍历产品从哈希? (亚马逊API /真空)亚马逊、遍历、扶手、真空

2023-09-11 11:23:43 作者:把最初的你还给我

扩展在Rails:如何提取哈希值? (亚马逊API /真空) - ?我怎么提供给我的意见,这些新挖出来的值

目前越来越

 未定义的方法`IMAGE_URL'的#<哈希:0x848402e0>

提取的源(左右线5#):
&其中;%=的link_to image_tag(product.image_url),product.url%​​GT;
 

main_controller.rb

 类MainController<的ApplicationController
  高清指数
    请求= Vacuum.new('国标')

    request.configure(
      aws_access_key_id:ABCDEFGHIJKLMNOPQRST,
      aws_secret_access_key:'<长期混乱的关键>',
      associate_tag:lipsum-20
    )

    PARAMS = {
      SearchIndex'=> '图书',
      '关键词'=> Ruby on Rails的',
      ResponseGroup'=> ItemAttributes中,图像
    }

    raw_products = request.item_search(查询:​​PARAMS)
    hashed_products = raw_products.to_h

    #把hashed_products ['ItemSearchResponse'] ['商品'] ['项目']收集{|。我|我['ItemAttributes中'] ['标题']}
    #把hashed_products ['ItemSearchResponse'] ['商品'] ['项目']收集{|。我|我['DetailPageURL']}
    #把hashed_products ['ItemSearchResponse'] ['商品'] ['项目']收集{|。我|我['LargeImage] [网址]}

    @products = []

    @products = hashed_products ['ItemSearchResponse'] ['商品'] ['项目']每做|。项目|
      产品= OpenStruct.new
      product.name =项目['ItemAttributes中'] ['标题']
      product.url =项目['DetailPageURL']
      product.image_url =项目['LargeImage'] ['URL']

      @products<<产品
    结束
  结束
结束
 

index.html.erb

 < H1>从亚马逊商品广告API&LT产品; / H1>
<%@如果products.any? %>
  <%@ products.each办|产品| %>
    < D​​IV CLASS =产品>
      &其中;%=的link_to image_tag(product.image_url),product.url%​​GT;
      &其中;%=的link_to product.name,product.url%​​GT;
    < / DIV>
  <%结束%GT;
<%结束%GT;
 
别再一知半解啦 索引其实就这么回事

解决方案

我不会把整个亚马逊散列成 OpenStruct 。而且,由于 product.name 你不能在上面做一个查找。

相反,通过亚马逊的项目只是循环,将它们分配给您的产品,然后添加到 @products 数组:

  @products = []
hashed_products ['ItemSearchResponse'] ['商品'] ['项目']每做|。项目|
  产品= OpenStruct.new
  product.name =项目['ItemAttributes中'] ['标题']
  @products<<产品
结束
 

Expanding on Rails: How to extract values from hash? (Amazon API / Vacuum) -- how do I make these newly dug up values available to my views?

Currently getting:

undefined method `image_url' for #<Hash:0x848402e0>

Extracted source (around line #5):
<%= link_to image_tag(product.image_url), product.url %> 

main_controller.rb:

class MainController < ApplicationController
  def index
    request = Vacuum.new('GB')

    request.configure(
      aws_access_key_id: 'ABCDEFGHIJKLMNOPQRST',
      aws_secret_access_key: '<long messy key>',
      associate_tag: 'lipsum-20'
    )

    params = {
      'SearchIndex' => 'Books',
      'Keywords'=> 'Ruby on Rails',
      'ResponseGroup' => "ItemAttributes,Images"
    }

    raw_products = request.item_search(query: params)
    hashed_products = raw_products.to_h

    # puts hashed_products['ItemSearchResponse']['Items']['Item'].collect{ |i| i['ItemAttributes']['Title'] }
    # puts hashed_products['ItemSearchResponse']['Items']['Item'].collect{ |i| i['DetailPageURL'] }
    # puts hashed_products['ItemSearchResponse']['Items']['Item'].collect{ |i| i['LargeImage']['URL'] }

    @products = []

    @products = hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
      product = OpenStruct.new
      product.name = item['ItemAttributes']['Title']
      product.url = item['DetailPageURL']
      product.image_url = item['LargeImage']['URL']

      @products << product 
    end
  end
end

index.html.erb:

<h1>Products from Amazon Product Advertising API</h1>
<% if @products.any? %>
  <% @products.each do |product| %>
    <div class="product">
      <%= link_to image_tag(product.image_url), product.url %>
      <%= link_to product.name, product.url %>
    </div>
  <% end %>
<% end %>

解决方案

I wouldn't turn the entire Amazon hash into an OpenStruct. And since product.name is nil you can't do a find on it.

Instead, just loop through the Amazon items, assign them to your product, and then add to the @products array:

@products = []
hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
  product = OpenStruct.new
  product.name = item['ItemAttributes']['Title']
  @products << product 
end