ruby-openidのテスト – ca14

openid用のモジュール ruby-openid をインストール後、添付のサンプルアプリケーションを実行してみる。

■gemでruby-openidをインストール

C:\>gem install ruby-openid
Successfully installed ruby-openid-2.1.7
1 gem installed
Installing ri documentation for ruby-openid-2.1.7…
Installing RDoc documentation for ruby-openid-2.1.7…

C:\>

■サンプルアプリケーションをコピー

以下のフォルダを任意のフォルダにコピーする。

C:\Ruby\lib\ruby\gems\1.8\gems\ruby-openid-2.1.7\examples\rails_openid

■セッションCookieの設定を追加

サンプルアプリコードでは、Rails2.0以降で利用するためのセッションCookieの設定が記載されていないため、config\environment.rbを編集

Ruby

# Be sure to restart your web server when you modify this file.

# Uncomment below to force Rails into production mode when
# you don't control web/app server and can't set it the proper way
# ENV['RAILS_ENV'] ||= 'production'

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
  # Settings in config/environments/* take precedence those specified here

  # Skip frameworks you're not going to use
  # config.frameworks -= [ :action_web_service, :action_mailer ]

  # Add additional load paths for your own custom dirs
  # config.load_paths += %W( #{RAILS_ROOT}/extras )

  # Force all environments to use the same logger level
  # (by default production uses :info, the others :debug)
  # config.log_level = :debug

  # Use the database for sessions instead of the file system
  # (create the session table with 'rake create_sessions_table')
  # config.action_controller.session_store = :active_record_store

	config.action_controller.session = {
	    :key => '_openid_session',
	    :secret      => 'c67504f48lfjaldkjaeiroeldakdhfkada'
	  }

  # Enable page/fragment caching by setting a file-based store
  # (remember to create the caching directory and make it readable to the application)
  # config.action_controller.fragment_cache_store = :file_store, "#{RAILS_ROOT}/cache"

  # Activate observers that should always be running
  # config.active_record.observers = :cacher, :garbage_collector

  # Make Active Record use UTC-base instead of local time
  # config.active_record.default_timezone = :utc

  # Use Active Record's schema dumper instead of SQL when creating the test database
  # (enables use of different database adapters for development and test environments)
  # config.active_record.schema_format = :ruby

  # See Rails::Configuration for more options
end

# Add new inflection rules using the following format
# (all these examples are active by default):
# Inflector.inflections do |inflect|
#   inflect.plural /^(ox)$/i, '\1en'
#   inflect.singular /^(ox)en/i, '\1'
#   inflect.irregular 'person', 'people'
#   inflect.uncountable %w( fish sheep )
# end

# Include your application configuration below
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:session_key] = '_session_id_2'

▲27~30行を追加

最初、Rails::Initializer.run メソッド内に記述していなかったため、軽くはまった。。。

■実行結果

サンプルアプリディレクトリでWeblickを実行

C:\rails_project\openid\rails_openid>ruby script/server
=> Booting WEBrick
=> Rails 2.3.3 application starting on http://0.0.0.0:3000

*******************************************************************
* config.breakpoint_server has been deprecated and has no effect. *
*******************************************************************

=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-02-02 09:40:38] INFO WEBrick 1.3.1
[2010-02-02 09:40:38] INFO ruby 1.8.6 (2008-08-11) [i386-mswin32]
[2010-02-02 09:40:38] INFO WEBrick::HTTPServer#start: pid=5636 port=3000


▲http://localhost:3000/consumer に接続

無料サーバ監視サービス – ca013

■死活監視用の静的HTMLの準備

publicフォルダに静的HTMLを準備する。

HTML

<!-- cpro01\public\ping.html -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    Test Contents
  </body>
</html>

■サーバ監視サービスに登録

監視用のURLを以下のサイトに登録

無料でも使えるIPv6対応の一歩進んだサーバ監視サービス – サイトアイ | SITE EYE

■参考サイト

その他のサービスは以下のURL参照

「サーバー監視サービス」をざっくり集めてみました *{M}+

個別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 %>

■実行結果

地図用のscaffold作成 – ca011

地図用のscaffoldを作成した。

■scaffoldの実行

属性ペアに入力したのは以下の通り

Plain Text

book_id:integer user_id:integer la:decimal lo:decimal title:text comment:text tag:text netabare_flg:integer kokai_flg:integer nendai:string memo:text del_flg:integer

Plain Text

      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/maps
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      exists  test/unit/helpers/
      exists  public/stylesheets/
      create  app/views/maps/index.html.erb
      create  app/views/maps/show.html.erb
      create  app/views/maps/new.html.erb
      create  app/views/maps/edit.html.erb
      create  app/views/layouts/maps.html.erb
   identical  public/stylesheets/scaffold.css
      create  app/controllers/maps_controller.rb
      create  test/functional/maps_controller_test.rb
      create  app/helpers/maps_helper.rb
      create  test/unit/helpers/maps_helper_test.rb
       route  map.resources :maps
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/map.rb
      create    test/unit/map_test.rb
      create    test/fixtures/maps.yml
      exists    db/migrate
      create    db/migrate/20100118123851_create_maps.rb

▲各種ファイルの作成

■migrateファイルの編集

座標用のフィールド属性には追加したい事があるので、migrate用ファイルを編集する。

Ruby

# ---- cpro01\db\migrate\20100118123851_create_maps.rb ----
class CreateMaps < ActiveRecord::Migration
  def self.up
    create_table :maps do |t|
      t.integer :book_id
      t.integer :user_id
      t.decimal :la, :precision => 18, :scale => 14, :default => 0
      t.decimal :lo, :precision => 18, :scale => 14, :default => 0
      t.text :title
      t.text :comment
      t.text :tag
      t.integer :netabare_flg
      t.integer :kokai_flg
      t.string :nendai
      t.text :memo
      t.integer :del_flg

      t.timestamps
    end
  end

  def self.down
    drop_table :maps
  end
end

▲7,8行目を編集

■テーブル追加

rake db:migrateを実行

Plain Text

(in C:/Documents and Settings/so-ten/My Documents/NetBeansProjects/cpro01)
==  CreateMaps: migrating =====================================================
-- create_table(:maps)
   -> 0.1570s
==  CreateMaps: migrated (0.1570s) ============================================

■実行結果

検索入力要素を元に、検索条件文言や再検索用情報を返すメソッドを追加 – ca010

■新規メソッドの作成

Controllerから、検索分類や検索順情報を受け取り、表示用の文言やデフォルトチェック項目を返すメソッドを作成。

Ruby

# ---- cpro01\lib\search_index.rb ----

def makerb(rbindex,rbsort)
  h1 = {"Books" => false, "DVD" => false, "Music" => false, "Software" => false, "VideoGames" => false}
  h2 = {"salesrank" => false, "releasedate" => false, "titlerank" => false, "-titlerank" => false}

  #デフォルトチェック用
  h1[rbindex] = true
  h2[rbsort] = true

  #検索した分類の表示文言
  case rbindex
  when "Books"
    se = "本"
  when "DVD"
    se = "DVD"
  when "Music"
    se = "音楽"
  when "Software"
    se = "ソフトウェア"
  when "VideoGames"
    se = "ゲーム"
  else
    se = "None"
  end

  #検索した分類の表示文言
  case rbsort
  when "salesrank"
    so = "売れている順"
  when "releasedate"
    so = "発売日順"
  when "titlerank"
    so = "アルファベット順(AからZへ) "
  when "-titlerank"
    so = "アルファベット順(ZからAへ) "
  else
    so = "None"
  end

  return h1, h2, se, so
end

■Controllerの編集

メソッド化したところをシンプルに

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
    @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

▲89~100行目を編集

■Veiwの編集

検索時に入力した文言を表示。ついでに再検索用フォームも追加

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 :controller => "maps", :action => "show" do %>
          <%= hidden_field_tag("asin", item.asin) %>
          <%= hidden_field_tag("address", "新宿") %>
          <%= submit_tag("このアイテムの地図データを登録") %>
      <% end %>
    </td>
  </tr>
  <% end %>
<% end %>
</table>

▲2~23行目を追加

■実行結果

ライブラリ追加のテスト – ca009

railsのライブラリ追加ルールを考えてみる。

■ライブラリファイルを作成

引数に2を加えて返すだけの関数を追加

Ruby

# ---- cpro01\lib\search_index.rb ----

def pu(d)
  return(d + 2)
end

■ライブラリ初期化用ファイルの作成

初期化時に上記ライブラリファイルを読み込むextensions.rbを作成

Ruby

# ---- cpro01\config\initializers\extensions.rb ----

require 'search_index.rb'

■Controllerで関数呼び出し

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
    @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 = {"Books" => false, "DVD" => false, "Music" => false, "Software" => false, "VideoGames" => false}
    @sortck = {"salesrank" => false, "releasedate" => false, "titlerank" => false, "-titlerank" => false}

    @keyword = params[:keyword]
    @search_index = params[:search_index]
    @sort = params[:sort]

    #デフォルトチェック用
    @searchck["#{@search_index}"] = true
    @sortck["#{@sort}"] = true

    #本だけはソート用のキーが他の物と違うため変更
    @sort = "daterank" if @search_index == "Books" && @sort == "releasedate"

    #検索
    res = Amazon::Ecs.item_search(@keyword, {
      :search_index => @search_index,
      :response_group => 'Medium',
      :sort => @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

    @testp = pu(5)
  end

end

▲追加したのは、最後の”@testp = pu(5)”のみ

■Viewファイルに追加・確認

@testpを表示してみる。

Ruby

<% # ---- cpro01\app\views\books\search.html.erb ---- %>
<%= h @testp %>
<p>検索結果</p>
<% 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 :controller => "maps", :action => "show" do %>
          <%= hidden_field_tag("asin", item.asin) %>
          <%= hidden_field_tag("address", "新宿") %>
          <%= submit_tag("この本の地図データを登録") %>
      <% end %>
    </td>
  </tr>
  <% end %>
<% end %>
</table>

▲2行目を追加

検索結果の上に”7”が表示される事を確認。

Amazon APIの検索バリエーションを増やす – ca008

キーワード以外にも、「本」「DVD」といった項目、および「売れている順」「発売日順」などのソート順を指定してAmazon検索ができるように拡張する。

■検索用フォームの追加

cpro01\app\views\top\index.html.erbを編集し、検索入力項目を追加する。

Ruby

<% # ---- cpro01\app\views\top\index.html.erb ---- %>

<h1>Top#index</h1>
<p>Find me in app/views/top/index.html.erb</p>
<%= @gmap.div(:width => 550, :height => 400) %>

<p>Amazonから登録図書を検索</p>
<% form_tag :controller => 'books', :action => 'search' do %>
  タイトル:<%= text_field_tag "keyword", @keyword %><br />
    分類:<%= radio_button_tag "search_index", "Books", checked=true %>本
  <%= radio_button_tag "search_index", "DVD" %>DVD
  <%= radio_button_tag "search_index", "Music" %>音楽
  <%= radio_button_tag "search_index", "Software" %>ソフトウェア
  <%= radio_button_tag "search_index", "VideoGames" %>ゲーム<br />

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

■BooksControllerを編集

入力項目処理を追加

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
    @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 = {"Books" => false, "DVD" => false, "Music" => false, "Software" => false, "VideoGames" => false}
    @sortck = {"salesrank" => false, "releasedate" => false, "titlerank" => false, "-titlerank" => false}

    @keyword = params[:keyword]
    @search_index = params[:search_index]
    @sort = params[:sort]

    #デフォルトチェック用
    @searchck["#{@search_index}"] = true
    @sortck["#{@sort}"] = true

    #本だけはソート用のキーが他の物と違うため変更
    @sort = "daterank" if @search_index == "Books" && @sort == "releasedate"

    #検索
    res = Amazon::Ecs.item_search(@keyword, {
      :search_index => @search_index,
      :response_group => 'Medium',
      :sort => @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

■検索結果を変更

次の地図登録ページへの遷移用のボタンを追加

Ruby

<% # ---- cpro01\app\views\books\search.html.erb ---- %>

<p>検索結果</p>
<% 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 :controller => "maps", :action => "show" do %>
          <%= hidden_field_tag("asin", item.asin) %>
          <%= hidden_field_tag("address", "新宿") %>
          <%= submit_tag("この本の地図データを登録") %>
      <% end %>
    </td>
  </tr>
  <% end %>
<% end %>
</table>

Amazon APIからのデータ取得 – ca007

■API Key他の初期設定

Ruby

# ---- cpro01\config\environment.rb ----

# Be sure to restart your server when you modify this file

# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
  # Settings in config/environments/* take precedence over those specified here.
  # Application configuration should go into files in config/initializers
  # -- all .rb files in that directory are automatically loaded.

  # Add additional load paths for your own custom dirs
  # config.load_paths += %W( #{RAILS_ROOT}/extras )

  # Specify gems that this application depends on and have them installed with rake gems:install
  # config.gem "bj"
  # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
  # config.gem "sqlite3-ruby", :lib => "sqlite3"
  # config.gem "aws-s3", :lib => "aws/s3"

  # Only load the plugins named here, in the order given (default is alphabetical).
  # :all can be used as a placeholder for all plugins not explicitly named
  # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

  # Skip frameworks you're not going to use. To use Rails without a database,
  # you must remove the Active Record framework.
  # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

  # Activate observers that should always be running
  # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

  # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
  # Run "rake -D time" for a list of tasks for finding time zone names.
  config.time_zone = 'UTC'

  # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
  # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
  # config.i18n.default_locale = :de

  # amazon-ecs
  require 'amazon/ecs'
  Amazon::Ecs.debug = true
  Amazon::Ecs.options = {
    :aWS_access_key_id => "foo",
    :associate_tag => "bar-22",
    :country => :jp,
    :aWS_secret_key => "baz"
  }
end

▲44~52行目を追加

■Modelの生成

ファイルのみ作成(generaterで生成するとActiveRecordを継承するため、利用しない)

Ruby

# ---- cpro01\app\models\item.rb ----

class Item
  attr_accessor :asin, :title, :url, :image, :author
  def initialize(asin, title = nil, url = nil, image = nil, author = nil)
    @asin = asin
    @title = title
    @url = url
    @image = image
    @author = author
  end
end

■BooksControllerの編集

searchメソッドを作成し、Amazon APIからデータを取得する。

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
    @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
    #キーワード取得
    @keyword = params[:keyword]

    #検索用初期値
    @search_index = "Books"
    @sort = "daterank"

    #検索
    res = Amazon::Ecs.item_search(@keyword, {
      :search_index => @search_index,
      :response_group => 'Medium',
      :sort => @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

▲88~112行目を追加

■Viewファイルの作成

search.html.erbを手動で新規作成。

Ruby

<%# ---- cpro01\app\views\books\search.html.erb ---- %>

<p>検索結果</p>
<% 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 %>
    </td>
  </tr>
  <% end %>
<% end %>
</table>

■検索用キーワード入力欄を追加

topのindex.html.erbに追加

Ruby

<% # ---- cpro01\app\views\top\index.html.erb ---- %>

<h1>Top#index</h1>
<p>Find me in app/views/top/index.html.erb</p>
<%= @gmap.div(:width => 550, :height => 400) %>

<p>Amazonから登録図書を検索</p>
<% form_tag :controller => 'books', :action => 'search' do %>
  タイトル:<%= text_field_tag "keyword", @keyword %><br />
  <%= submit_tag "アマゾンで検索" %><br />
<% end %>

▲7~11行目を追加

erbファイルのコメントに関して – ca006

ちょっとはまったので。。。

■コメント記述方法

Ruby

#OK
<% # ---- cpro01\app\views\top\index.html.erb ---- %>

#NG
<%=# ---- cpro01\app\views\top\index.html.erb ---- %>

■NG時のエラーメッセージ

Plain Text

 SyntaxError in Top#index

Showing app/views/top/index.html.erb where line #7 raised:

compile error
C:/Documents and Settings/so-ten/My Documents/NetBeansProjects/cpro01/app/views/top/index.html.erb:7: syntax error, unexpected kENSURE, expecting ')'
C:/Documents and Settings/so-ten/My Documents/NetBeansProjects/cpro01/app/views/top/index.html.erb:9: syntax error, unexpected kEND, expecting ')'

Extracted source (around line #7):

4: <p>Find me in app/views/top/index.html.erb</p>
5: <%= @gmap.div(:width => 550, :height => 400) %>

Trace of template inclusion: app/views/top/index.html.erb

RAILS_ROOT: C:/Documents and Settings/so-ten/My Documents/NetBeansProjects/cpro01
Application Trace | Framework Trace | Full Trace

C:/Documents and Settings/so-ten/My Documents/NetBeansProjects/cpro01/app/views/top/index.html.erb:9:in `compile!'

C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/renderable.rb:61:in `compile'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/renderable.rb:28:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/template.rb:199:in `render_template'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:260:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:343:in `_render_with_layout'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:257:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1246:in `render_for_file'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:941:in `render_without_benchmark'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:51:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `ms'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:10:in `realtime'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `ms'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:51:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1322:in `default_render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1328:in `perform_action_without_filters'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:617:in `call_filters'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `ms'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:10:in `realtime'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `ms'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/flash.rb:146:in `perform_action'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:527:in `send'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:527:in `process_without_filters'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:606:in `process'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:391:in `process'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:386:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/routing/route_set.rb:434:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:88:in `dispatch'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:111:in `_call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:82:in `initialize'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:29:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:29:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:9:in `cache'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:28:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/head.rb:9:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/methodoverride.rb:24:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/params_parser.rb:15:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/session/cookie_store.rb:93:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/reloader.rb:29:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/failsafe.rb:26:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in `synchronize'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:106:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/rails/rack/static.rb:31:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:46:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in `each'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/rails/rack/log_tailer.rb:17:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/content_length.rb:13:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/webrick.rb:46:in `service'
C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/webrick.rb:13:in `run'
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/commands/server.rb:111
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3

C:/Documents and Settings/so-ten/My Documents/NetBeansProjects/cpro01/app/views/top/index.html.erb:9:in `compile!'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/renderable.rb:61:in `compile'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/renderable.rb:28:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/template.rb:199:in `render_template'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:260:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:343:in `_render_with_layout'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:257:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1246:in `render_for_file'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:941:in `render_without_benchmark'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:51:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `ms'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:10:in `realtime'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `ms'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:51:in `render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1322:in `default_render'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1328:in `perform_action_without_filters'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:617:in `call_filters'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `ms'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:10:in `realtime'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `ms'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/flash.rb:146:in `perform_action'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:527:in `send'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:527:in `process_without_filters'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:606:in `process'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:391:in `process'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:386:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/routing/route_set.rb:434:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:88:in `dispatch'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:111:in `_call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:82:in `initialize'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:29:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:29:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:9:in `cache'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:28:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/head.rb:9:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/methodoverride.rb:24:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/params_parser.rb:15:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/session/cookie_store.rb:93:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/reloader.rb:29:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/failsafe.rb:26:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in `synchronize'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:106:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/rails/rack/static.rb:31:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:46:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in `each'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/rails/rack/log_tailer.rb:17:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/content_length.rb:13:in `call'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/webrick.rb:46:in `service'
C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/webrick.rb:13:in `run'
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/commands/server.rb:111
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3

Request

Parameters:

None

Show session dump

--- 

Response

Headers:

{"Content-Type"=>"",
 "Cache-Control"=>"no-cache"}

topページ表示用の初期ルーティング設定 – ca006

■index.htmlの削除

以下のファイルを削除

Plain Text

cpro01/public/index.html

■routes.rb

Ruby

# ---- cpro01\config\routes.rb ----

ActionController::Routing::Routes.draw do |map|
  map.resources :books

  # The priority is based upon order of creation: first created -> highest priority.

  # Sample of regular route:
  #   map.connect 'products/:id', :controller => 'catalog', :action => 'view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   map.resources :products

  # Sample resource route with options:
  #   map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }

  # Sample resource route with sub-resources:
  #   map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller

  # Sample resource route with more complex sub-resources
  #   map.resources :products do |products|
  #     products.resources :comments
  #     products.resources :sales, :collection => { :recent => :get }
  #   end

  # Sample resource route within a namespace:
  #   map.namespace :admin do |admin|
  #     # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
  #     admin.resources :products
  #   end

  # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
  # map.root :controller => "welcome"
  map.root :controller => "top"

  # See how all your routes lay out with "rake routes"

  # Install the default routes as the lowest priority.
  # Note: These default routes make all actions in every controller accessible via GET requests. You should
  # consider removing or commenting them out if you're using named routes and resources.
  map.connect ':controller/:action.:format'
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

▲39,46行目を追加