问题描述
我是 Ruby 和 Ruby on Rails 的新手,正在 Max OSX Lion 上使用 Ruby 1.9.2 和 Rails 3.2.5,并且正在阅读使用 Rails 进行敏捷 Web 开发(第 4 版)"一书.
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)".
我使用第 6 章中概述的以下命令创建了他们的示例 depot 应用程序:
I've created their sample depot app using the following commands outlined in Chapter 6:
- 铁路新仓库
- rails 生成脚手架 Product title:string description:text image_url:string price:decimal
- 耙数据库:迁移
- rails 服务器
当我将 Safari 指向http://localhost:3000/products"时,我收到以下操作控制器:异常捕获错误消息,而不是看到产品列表页面:
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.
在终端中运行rake 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
以下行已自动添加到 routes.rb.
The following line was automatically added to routes.rb.
resources: product
products_controller.rb 中也有索引方法.
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.
提前致谢.
推荐答案
想通了 - 我在另一个终端窗口中运行了另一个 WEBrick 服务器实例,但它用于上一章的演示应用程序.
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.
吸取教训.确保从要测试的 Rails 应用程序的目录运行本地服务器.
Lesson learned. Make sure you run the local server from the directory of the Rails application you want to test.
这篇关于为什么我得到 No Route Matches [GET] “/products"?路线什么时候存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!