Why do I get a No Route Matches [GET] “/products” when the route exists?

你。 提交于 2019-12-11 10:22:22

问题


I'm new to Ruby and Ruby on Rails, am using Ruby 1.9.2 and Rails 3.2.5 on Max OSX Lion, and am working my way through the book "Agile Web Development with Rails (4th Edition)".

I've created their sample depot app using the following commands outlined in Chapter 6:

  1. rails new depot
  2. rails generate scaffold Product title:string description:text image_url:string price:decimal
  3. rake db:migrate
  4. rails server

When I point Safari to "http://localhost:3000/products" I get the following Action Controller: Exception Caught error message instead of seeing a product list page:

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

Running "rake routes" in Terminal gives me:

    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

The following line was automatically added to routes.rb.

resources: product

There is also an index method in 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

A search of the book's errata and forums did not turn up any possible solutions.

Thanks in advance.


回答1:


Figured it out - I had another instance of the WEBrick server running in another terminal window but it was for a demo application from a previous chapter.

Lesson learned. Make sure you run the local server from the directory of the Rails application you want to test.



来源:https://stackoverflow.com/questions/10965608/why-do-i-get-a-no-route-matches-get-products-when-the-route-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!