7

我觉得奇怪ActionController::RoutingError: No route matches。可以生成路由:

> r = Rails.application.routes
> r.generate controller: :items, action: :index, user_id:1
  => ["/users/1/items", {}] 

它位于rake routes

$ rake routes
     user_items GET    /users/:user_id/items(.:format)          items#index
                POST   /users/:user_id/items(.:format)          items#create
  new_user_item GET    /users/:user_id/items/new(.:format)      items#new
 edit_user_item GET    /users/:user_id/items/:id/edit(.:format) items#edit
      user_item GET    /users/:user_id/items/:id(.:format)      items#show
                PUT    /users/:user_id/items/:id(.:format)      items#update
                DELETE /users/:user_id/items/:id(.:format)      items#destroy
(...)

但我不承认:

r.recognize_path "/users/1/items"
ActionController::RoutingError: No route matches "/users/1/items"

我的routes.rb

Sumo2::Application.routes.draw do
  resources :users do
    resources :items
  end
  (...)
end

有任何想法吗?

4

1 回答 1

8

你有ItemsController定义类吗?没有它,您的路线将不会被识别。也许你拼错了。

irb(main):001:0> r = Rails.application.routes
=> #<ActionDispatch::Routing::RouteSet:0x1cbba00>
irb(main):002:0> r.recognize_path "/users/1/items"
ActionController::RoutingError: No route matches "/users/1/items"
        ... errors ...
irb(main):003:0> class ItemsController; end # <----------<
=> nil
irb(main):004:0> r.recognize_path "/users/1/items"
=> {:action=>"index", :controller=>"items", :user_id=>"1"}
于 2012-06-12T10:18:44.847 回答