個別amazonアイテム表示ページの作成 – ca012

個別のamazonアイテム表示ページを作成

■検索結果Viewの編集

引き渡すasinコードを指定

Ruby

<% # ---- cpro01\app\views\books\search.html.erb ---- %>
<p>検索結果</p>
タイトル:<%= h params[:keyword] %><br />
分類:<%= h @search_v %><br />
検索順:<%= h @sort_v %><br />
<br />
再検索<br />
<% form_tag :action => 'search' do %>
  タイトル:<%= text_field_tag "keyword", params[:keyword] %><br />
    分類:<%= radio_button_tag "search_index", "Books", checked=@searchck["Books"] %>本
  <%= radio_button_tag "search_index", "DVD", checked=@searchck["DVD"] %>DVD
  <%= radio_button_tag "search_index", "Music", checked=@searchck["Music"] %>音楽
  <%= radio_button_tag "search_index", "Software", checked=@searchck["Software"] %>ソフトウェア
  <%= radio_button_tag "search_index", "VideoGames", checked=@searchck["VideoGames"] %>ゲーム<br />

  検索順:<%= radio_button_tag "sort", "salesrank", checked=@sortck["salesrank"] %>売れている順
  <%= radio_button_tag "sort", "releasedate", checked=@sortck["releasedate"] %>発売日順
  <%= radio_button_tag "sort", "titlerank", checked=@sortck["titlerank"] %>アルファベット順(AからZへ)
  <%= radio_button_tag "sort", "-titlerank", checked=@sortck["-titlerank"] %>アルファベット順(ZからAへ)<br />
  <%= submit_tag "アマゾンで再検索" %><br />
<% end %>
<br />
<br />

<% if @items && @items.size > 0 %>
<table>
  <% @items.each do |item| %>

  <tr>
    <td><%= item.title %><br />
        <% if item.image != nil %>
          <%= link_to(image_tag(item.image[:url], :size => "#{item.image[:width]}x#{item.image[:height]}", :alt => item.title), item.url, :target => '_blank') %>
        <% end %><br />
        <%= item.asin %><br />
        <%= item.author %>
      <% form_tag :action => "show" do %>
          <%= hidden_field_tag("asin", item.asin) %>
          <%= submit_tag("このアイテムの地図データを登録") %>
      <% end %>
    </td>
  </tr>
  <% end %>
<% end %>
</table>

▲36~39行目を編集

■books_controllerのshowメソッドを編集

Amazon::Ecsで個別アイテムの情報を取得

Ruby

# ---- cpro01\app\controllers\books_controller.rb ----

class BooksController < ApplicationController
  # GET /books
  # GET /books.xml
  def index
    @books = Book.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @books }
    end
  end

  # GET /books/1
  # GET /books/1.xml
  def show
    #amazon検索
    @res = Amazon::Ecs.item_lookup(params[:asin], {
    :response_group => 'Medium',
    }).items[0]

#    @book = Book.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @book }
    end
  end

  # GET /books/new
  # GET /books/new.xml
  def new
    @book = Book.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @book }
    end
  end

  # GET /books/1/edit
  def edit
    @book = Book.find(params[:id])
  end

  # POST /books
  # POST /books.xml
  def create
    @book = Book.new(params[:book])

    respond_to do |format|
      if @book.save
        flash[:notice] = 'Book was successfully created.'
        format.html { redirect_to(@book) }
        format.xml  { render :xml => @book, :status => :created, :location => @book }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /books/1
  # PUT /books/1.xml
  def update
    @book = Book.find(params[:id])

    respond_to do |format|
      if @book.update_attributes(params[:book])
        flash[:notice] = 'Book was successfully updated.'
        format.html { redirect_to(@book) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /books/1
  # DELETE /books/1.xml
  def destroy
    @book = Book.find(params[:id])
    @book.destroy

    respond_to do |format|
      format.html { redirect_to(books_url) }
      format.xml  { head :ok }
    end
  end

  def search
    #検索結果文言と再検索用の設定値を取得
    @searchck,@sortck,@search_v,@sort_v = makerb(params[:search_index],params[:sort])

    #本だけは「発売日順」でのソート用キーが他の物と違うため変更
    params[:sort] = "daterank" if params[:search_index] == "Books" && params[:sort] == "releasedate"

    #Amazon商品検索
    res = Amazon::Ecs.item_search(params[:keyword], {
      :search_index => params[:search_index],
      :response_group => 'Medium',
      :sort => params[:sort]
    })
    @items = []
    res.items.each do |item|
      @items << Item.new(
        item.get('asin'),
        item.get('itemattributes/title'),
        item.get('detailpageurl'),
        item.get_hash('smallimage'),
        item.get('author')
      )
    end
  end
end

▲18~23行目を追加

21行目で、items[0]に最初の要素を返すのがポイント

■ShowのViewを編集

Ruby

<% # ---- cpro01\app\views\books\show.html.erb ---- %>
<p>
  <b>res:</b>
<table>
  <tr>
    <td Valign="top">
        <% if @res.get('mediumimage') != nil %>
          <%= link_to(image_tag(@res.get_hash('mediumimage')[:url], :alt => @res.get('title')), @res.get('detailpageurl'), :target => '_blank') %>
        <% end %>
    </td>
  </tr>
  <tr><td><%= @res.get('title') %></td></tr>
  <tr><td><%= @res.get_array('author').join(",") %></td></tr>
  <tr><td><%= @res.get('publisher') %></td></tr>
</table>
</p>

<p>
  <b>Asin:</b>
  <%#=h @book.asin %>
</p>

<p>
  <b>Del flg:</b>
  <%#=h @book.del_flg %>
</p>

<%#= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>

■実行結果


Post a Comment