红宝石和铁轨的资源路线

ste*_*hen 1 ruby routes ruby-on-rails helper

我有

资源:博客

在我的routes.rb文件中声明但是当我尝试访问控制器或html.erb文件中的blog_path时,我收到以下错误:

没有路由匹配{:controller =>"blog",:action =>"show"}缺少必需的密钥:[:id]

我创建了一个名为BlogController的控制器,并在views目录中使用show.html.erb文件定义了方法show.如果我定义:

匹配'/ blog',到:'博客#show',通过:'get'代替,然后blog_path工作正常.

我的理解是资源:博客只是匹配'/博客'的语法糖,:'博客#show',via:'get'和一堆其他路线.请帮忙.

Dan*_*sky 8

blog_path是用于生成到博客路径,所以需要id或博客对象,这个辅助像生成路径/blogs/12blogs#show,并且blogs#show是用于示出对象.blogs_path产生/blogsblogs#index(就像所有的博客).

查看2资源路由:Rails默认值

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
Run Code Online (Sandbox Code Playgroud)

您已经使用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
Run Code Online (Sandbox Code Playgroud)


Ahm*_*ain 5

像这个资源一样使资源复数:博客

并让控制器名称 blogs_controller.rb 和它的类名称 BlogsController

这是导轨标准