代码之家  ›  专栏  ›  技术社区  ›  rmagnum2002

脚手架路线不需要id

  •  1
  • rmagnum2002  · 技术社区  · 8 年前

    当我在rails 5应用程序中运行rake路由时,我看到以下可用的url:

    edit_articles     GET    /articles/edit(.:format)    articles#edit
    articles          GET    /articles(.:format)         articles#show
                      PATCH  /articles(.:format)         articles#update
                      PUT    /articles(.:format)         articles#update
                      DELETE /articles(.:format)         articles#destroy
    

    当我创建指向资源的链接时,我需要在其中包含id参数,如下所示:

    link_to article.name, manager_articles_path(id: article.id)
    

    而不是4向导轨:

    link_to article.name, manager_articles_path(article)
    

    edit_article GET    /articles/:id/edit(.:format) articles#edit
         article GET    /articles/:id(.:format)      articles#show
                 PATCH  /articles/:id(.:format)      articles#update
                 PUT    /articles/:id(.:format)      articles#update
                 DELETE /articles/:id(.:format)      articles#destroy
    

    非常感谢。

    Rails.application.routes.draw do
      root 'home#index'
      resource :articles
    end
    
    1 回复  |  直到 8 年前
        1
  •  4
  •   Ajay Barot    8 年前

    在轨道中 resource resources 不一样。

    资源

    http://guides.rubyonrails.org/routing.html#singular-resources

    有时,你有一个客户查找时总是没有的资源 当前登录用户的配置文件。在这种情况下,您可以使用 将/profile(而不是/profile/:id)映射到 显示操作。

    路线

     edit_articles     GET    /articles/edit(.:format)    articles#edit
     articles          GET    /articles(.:format)         articles#show
                       PATCH  /articles(.:format)         articles#update
                       PUT    /articles(.:format)         articles#update
                       DELETE /articles(.:format)         articles#destroy
    

    资源

    资源被用作处理任何项上的一般请求的方法,然后单一资源是处理当前项的方法。

    路线

    articles     GET    /articles(.:format)          articles#index
                 POST   /articles(.:format)          articles#create
     new_article GET    /articles/new(.:format)      articles#new
    edit_article GET    /articles/:id/edit(.:format) articles#edit
         article GET    /articles/:id(.:format)      articles#show
                 PATCH  /articles/:id(.:format)      articles#update
                 PUT    /articles/:id(.:format)      articles#update
                 DELETE /articles/:id(.:format)      articles#destroy
    

    我希望这对你有帮助。