ActionController :: RoutingError没有路由匹配[GET]“/ stories”

时间:2012-08-08 02:02:24

标签: ruby-on-rails rails-routing

操作系统: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服务器来启动服务器。 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)

2 个答案:

答案 0 :(得分:0)

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

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

rails generate resource ModelName fields:types

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

希望这有帮助!

答案 1 :(得分:0)

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

rails g controller stories index

rails g controller Story index

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

match "/stories" => "stories#index", :as => "stories"