1

操作系统:openSUSE 11.4 Rails 3.2.0

我刚刚创建了一个非常简单的应用程序“koko”。这是我运行的命令的顺序

rails new koko
rails generate model Story name:string description:string
rake db:migrate
rails generate controller Stories index

以上所有命令均已成功执行。然后我运行 =>rails server 来启动服务器。localhost:3000/ 工作正常。

当我尝试执行 localhost:3000/stores 时,我收到以下错误:(浏览器显示第一行关于无路由匹配)

Started GET "/stories" for 127.0.0.1 at Sat Aug 04 12:52:02 -0400 2012
ActionController::RoutingError (No route matches [GET] "/stories"):
  actionpack (3.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.0) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.0) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.0) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.0) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.0) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.0) lib/action_dispatch/middleware/static.rb:53:in `call'
  railties (3.2.0) lib/rails/engine.rb:479:in `call'
  railties (3.2.0) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.0) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  /usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
  /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
  /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
  /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
  rack (1.4.1) lib/rack/handler/webrick.rb:13:in `run'
  rack (1.4.1) lib/rack/server.rb:265:in `start'
  railties (3.2.0) lib/rails/commands/server.rb:70:in `start'
  railties (3.2.0) lib/rails/commands.rb:55
  railties (3.2.0) lib/rails/commands.rb:50:in `tap'
  railties (3.2.0) lib/rails/commands.rb:50
  script/rails:6:in `require'
  script/rails:6


Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.2.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.9ms)
4

2 回答 2

0

您的问题是您正在尝试对非 RESTful 资源使用 RESTful 接口。您制作了一个模型,并制作了一个带有索引操作的控制器 ( /controller_name/action)。

如果您对使用感到满意/stories/index,那么我们就完成了!如果您更喜欢更传统的 RESTful 界面/stories,那么我建议您使用:

rails generate resource ModelName fields:types.

这将创建一个模型和一个控制器,并且应该开箱即用地工作。这将设置评论中提到的路线。

希望这可以帮助!

于 2012-08-08T03:31:53.570 回答
0

您可能只是有一些命名问题。通常,在创建控制器时,它应该是

rails g controller stories index

或者

rails g controller Story index

然后将您自动生成的路线更改为

match "/stories" => "stories#index", :as => "stories"
于 2012-08-08T05:54:35.560 回答