【问题标题】:Why do I get a No Route Matches [GET] "/products" when the route exists?当路由存在时,为什么我会收到 No Route Matches [GET] "/products"?
【发布时间】:2012-06-10 00:38:31
【问题描述】:

我是 Ruby 和 Ruby on Rails 的新手,正在 Max OSX Lion 上使用 Ruby 1.9.2 和 Rails 3.2.5,并且正在阅读“Agile Web Development with Rails (4th Edition)”一书。

我已经使用第 6 章中概述的以下命令创建了他们的示例 depot 应用程序:

  1. rails 新仓库
  2. rails 生成脚手架 Product title:string description:text image_url:string price:decimal
  3. rake db:迁移
  4. Rails 服务器

当我将 Safari 指向“http://localhost:3000/products”时,我收到以下操作控制器:异常捕获错误消息,而不是看到产品列表页面:

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

在终端中运行“rake 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

搜索本书的errataforums 没有找到任何可能的解决方案。

提前致谢。

【问题讨论】:

    标签: ruby-on-rails-3 rails-routing


    【解决方案1】:

    想通了 - 我在另一个终端窗口中运行了另一个 WEBrick 服务器实例,但它是用于上一章的演示应用程序。

    经验教训。确保从要测试的 Rails 应用程序目录运行本地服务器。

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 2011-08-22
      • 2017-04-10
      • 2022-12-26
      相关资源
      最近更新 更多