0

I'm trying to fetch products from the Amazon Products API (using https://github.com/hakanensari/vacuum/) and display them in my Rails app. But how do I bring the product names and photos into my views?

Currently getting:

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 %>
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Mark Boulder
  • 13,577
  • 12
  • 48
  • 78
  • What is the type of `raw_products`? It apparently isn't an array or anything. Also, don't use `map!` if you are going to assign it to a different value. `map!` mutates the object you call it on, where `map` will just return the new value. – Justin Wood May 01 '14 at 13:55
  • @JustinWood: Should I instantiate it as an array first, ie. `raw_products = []`? I'm not sure how to debug the actual response (`Rails.logger.debug("My response: #{request.item_search(query: params)}")` didn't return anything). Here's a similar setup using `map!` with the ShopSense API -- https://gist.github.com/frankie-loves-jesus/11058955 -- should I switch to `map` there as well? – Mark Boulder May 01 '14 at 13:58

1 Answers1

1

You are getting the error because raw_products isn't an array. AS par vacuum documentation, you will either have to convert it to hash by raw_products.to_h or You can also pass the response body into your own parser for some custom XML heavy-lifting:

MyParser.new(raw_products.body) 

So you have to first parse the the response properly before consuming it.


You can just do following:

  @products = raw_products.to_h
  product = OpenStruct.new(@products)
Saurabh
  • 71,488
  • 40
  • 181
  • 244
  • Like this `raw_products.to_h`? Still getting undefined method `map!` or `map` though. Also I'm guessing there's no need for the heavy-lifting? – Mark Boulder May 01 '14 at 14:05
  • yes, now you may be getting this because, this is hash, I don't know in what exact format your results are, you can try to print them and then decide how to use it, If it is a map, you may not be needing map at all, you may directly start using it. – Saurabh May 01 '14 at 14:09
  • I tried printing it out with `Rails.logger.debug("My response: #{raw_products}"` but I just got `My response: #`. – Mark Boulder May 01 '14 at 14:10
  • Very cool! Thanks man! Any idea how to make it work with `<%= link_to image_tag(product.image.url), product.url %>` in the view though? Updated question above. – Mark Boulder May 01 '14 at 14:18
  • rails views, I am not sure how to render :( – Saurabh May 01 '14 at 14:28
  • `Rails.logger.debug("My response: #{@products}")` now successfully returns JSON. If you see my similar setup with the ShopSense API, you can see how it's rendered in views: https://gist.github.com/frankie-loves-jesus/11058955 -- do let me know if you can think of anything. – Mark Boulder May 01 '14 at 14:29
  • Migrated to http://stackoverflow.com/questions/23409950/how-do-display-amazon-product-api-in-my-rails-views – Mark Boulder May 01 '14 at 14:41