脚手架路线不希望有 id 的

scaffold routes do not expect to have id's

当我 运行 在 rails 5 应用程序中搜索路由时,我看到以下可用网址:

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

当我创建一个 link_to 资源时,我需要在其中包含 id 参数,例如:

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

而不是rails 4 种方式:

link_to article.name, manager_articles_path(article)

如何让 rails 5 条路线表现得像 rails 4 条路线?

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

谢谢。

routes.rb

Rails.application.routes.draw do
  root 'home#index'
  resource :articles
end

在rails中resourceresources是不一样的。

资源

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

Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action.

航线

 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

希望对您有所帮助。