问题
Here are my routes:
equipment_index GET /equipment(.:format) {:action=>"index", :controller=>"equipment"}
POST /equipment(.:format) {:action=>"create", :controller=>"equipment"}
new_equipment GET /equipment/new(.:format) {:action=>"new", :controller=>"equipment"}
edit_equipment GET /equipment/:id/edit(.:format) {:action=>"edit", :controller=>"equipment"}
equipment GET /equipment/:id(.:format) {:action=>"show", :controller=>"equipment"}
PUT /equipment/:id(.:format) {:action=>"update", :controller=>"equipment"}
DELETE /equipment/:id(.:format) {:action=>"destroy", :controller=>"equipment"}
categories GET /categories(.:format) {:action=>"index", :controller=>"categories"}
POST /categories(.:format) {:action=>"create", :controller=>"categories"}
new_category GET /categories/new(.:format) {:action=>"new", :controller=>"categories"}
edit_category GET /categories/:id/edit(.:format) {:action=>"edit", :controller=>"categories"}
category GET /categories/:id(.:format) {:action=>"show", :controller=>"categories"}
PUT /categories/:id(.:format) {:action=>"update", :controller=>"categories"}
DELETE /categories/:id(.:format) {:action=>"destroy", :controller=>"categories"}
When I go to http://localhost:3000/equipment/new I get the following error:
No route matches {:action=>"show", :controller=>"equipment"}
This is my routes.rb file:
Equipmentmanager::Application.routes.draw do
resources :equipment
resources :categories
end
Everything else are set to the defaults, except that I used nifty:scaffold.
This is in 3.1, but it does it in 3.0 also I am not sure what I am missing?
回答1:
So localhost:3000/equipment/new is getting routed with this line:
equipment GET /equipment/:id(.:format) {:action=>"show", :controller=>"equipment"}
As it should be. Since you want it to actually go to the new route you need to call localhost:3000/new_equipment since you have defined that route.
As a bit cleaner syntax if you wanted localhost:3000/new_equipment to route to the new action you could put this line in your routes.rb file (above the resources :equipment line since its more specific):
get "new_equipment" => "equipment#new", :as => "new_equipment"
That line will also define helpers that give you access to new_equipment_path and new_equipment_url
来源:https://stackoverflow.com/questions/6482274/new-action-trying-to-use-show