我遇到路线问题。我已经定义了这样的路线:
resource :demo
resource :subjects
resource :pages
resource :sections
当我做的时候
rake routes
它没有显示正确的网址。它显示了类似
的内容Prefix Verb URI Pattern Controller#Action
root GET / demo#index
demo POST /demo(.:format) demos#create
new_demo GET /demo/new(.:format) demos#new
edit_demo GET /demo/edit(.:format) demos#edit
GET /demo(.:format) demos#show
PATCH /demo(.:format) demos#update
PUT /demo(.:format) demos#update
DELETE /demo(.:format) demos#destroy
subjects POST /subjects(.:format) subjects#create
new_subjects GET /subjects/new(.:format) subjects#new
edit_subjects GET /subjects/edit(.:format) subjects#edit
GET /subjects(.:format) subjects#show
PATCH /subjects(.:format) subjects#update
PUT /subjects(.:format) subjects#update
DELETE /subjects(.:format) subjects#destroy
pages POST /pages(.:format) pages#create
new_pages GET /pages/new(.:format) pages#new
edit_pages GET /pages/edit(.:format) pages#edit
GET /pages(.:format) pages#show
PATCH /pages(.:format) pages#update
PUT /pages(.:format) pages#update
DELETE /pages(.:format) pages#destroy
sections POST /sections(.:format) sections#create
new_sections GET /sections/new(.:format) sections#new
edit_sections GET /sections/edit(.:format) sections#edit
GET /sections(.:format) sections#show
PATCH /sections(.:format) sections#update
PUT /sections(.:format) sections#update
DELETE /sections(.:format) sections#destroy
GET /:controller(/:action(/:id(.:format))) :controller#:action
没有网址包含:id。我可能做错了什么?它仍然向控制器发送id,但我很难调用索引和显示方法,因为它们都被映射到----- #show
答案 0 :(得分:3)
这是因为您使用的是singular resource,例如resource :foo
。当您使用单数资源时,您无法获得:id
。要在参数中获取:id
,您应该将资源声明更改为复数resources
:
resources :demoes
resources :subjects
resources :pages
resources :sections