我正在尝试从 Amazon Products API(使用https://github.com/hakanensari/vacuum/)获取产品并将它们显示在我的 Rails 应用程序中。但是如何将产品名称和照片带入我的视图?
目前获得:
ActionView::Template::Error (undefined method `image' for #<Array:0x88486aec>):
2: <% if @products.any? %>
3: <% @products.each do |product| %>
4: <div class="product">
5: <%= link_to image_tag(product.image.url), product.url %>
6: <%= link_to product.name, product.url %>
7: </div>
8: <% end %>
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'
}
#
# NOT SURE WHERE TO TAKE IT FROM HERE
#
raw_products = request.item_search(query: params)
@products = raw_products.to_h
product = OpenStruct.new(@products)
end
end
index.html.erb:
<% 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 %>