1

私はRubyとRubyonRailsを初めて使用し、Max OSXLionでRuby1.9.2とRails3.2.5を使用しており、「Railsを使用したアジャイルWeb開発(第4版)」という本を読んでいます。

第6章で概説した次のコマンドを使用して、サンプルデポアプリを作成しました。

  1. Railsの新しいデポ
  2. Railsは足場を生成します製品タイトル:文字列の説明:テキストimage_url:文字列価格:10進数
  3. rake db:migrate
  4. Railsサーバー

Safariで「http:// localhost:3000 / products」を指定すると、次のアクションコントローラーが表示されます。製品リストページが表示される代わりに、例外が発生しました。エラーメッセージが表示されます。

Routing Error
No route matches [GET] "/products"
Try running rake routes for more information on available routes.

ターミナルで「レーキルート」を実行すると、次のようになります。

    products GET    /products(.:format)          products#index
             POST   /products(.:format)          products#create
 new_product GET    /products/new(.:format)      products#new
edit_product GET    /products/:id/edit(.:format) products#edit
     product GET    /products/:id(.:format)      products#show
             PUT    /products/:id(.:format)      products#update
             DELETE /products/:id(.:format)      products#destroy

次の行がroutes.rbに自動的に追加されました。

resources: product

products_controller.rbにはインデックスメソッドもあります。

class ProductsController < ApplicationController
  # GET /products
  # GET /products.json
  def index
    @products = Product.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

この本の正誤表フォーラムを検索しても、考えられる解決策は見つかりませんでした。

前もって感謝します。

4

1 に答える 1

0

理解できた-別のターミナルウィンドウでWEBrickサーバーの別のインスタンスを実行していましたが、前の章のデモアプリケーション用でした。

学んだ教訓。テストするRailsアプリケーションのディレクトリからローカルサーバーを実行していることを確認してください。

于 2012-06-10T00:40:01.240 に答える