我有
resources :blog
在routes.rb
文件中声明但在尝试访问blog_path
控制器或html.erb
文件时,出现以下错误:
No route matches {:controller=>"blog", :action=>"show"} missing required keys: [:id]
我创建了一个名为的控制器,BlogController
并在views目录中show
使用show.html.erb
文件定义了该方法。如果我定义:
match '/blog', to: 'blog#show', via: 'get'
相反,blog_path
可以正常工作。
我的理解是资源:blog
仅仅是语法糖match '/blog', to: 'blog#show', via: 'get'
和其他许多途径。请帮忙。
blog_path
是用于生成到博客路径,所以需要id
或博客对象,这个辅助像生成路径/blogs/12
到blogs#show
,并且blogs#show
是用于示出对象。blogs_path
生成/blogs
到blogs#index
(就像所有博客一样)。
resources :photos
GET /photos index display a list of all photos
GET /photos/new new return an HTML form for creating a new photo
POST /photos create create a new photo
GET /photos/:id show display a specific photo
GET /photos/:id/edit edit return an HTML form for editing a photo
PATCH/PUT /photos/:id update update a specific photo
DELETE /photos/:id destroy delete a specific photo
您已经使用resources :blog
没有s
。它产生
blog_index GET /blog(.:format) blog#index
POST /blog(.:format) blog#create
new_blog GET /blog/new(.:format) blog#new
edit_blog GET /blog/:id/edit(.:format) blog#edit
blog GET /blog/:id(.:format) blog#show
PUT /blog/:id(.:format) blog#update
DELETE /blog/:id(.:format) blog#destroy
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句