本文介绍了扶手:如何遍历产品从哈希? (亚马逊API /真空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
扩展在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办|产品| %>
< DIV 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
这篇关于扶手:如何遍历产品从哈希? (亚马逊API /真空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!