脚手架路线不希望有id

时间:2016-09-26 05:57:51

标签: ruby-on-rails routes ruby-on-rails-5

当我在rails 5 app中运行rake路由时,我看到以下网址:

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 param,如:

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

1 个答案:

答案 0 :(得分:4)

在导轨resourceresources中不相同。

<强>资源

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

  

有时,您拥有一个客户端始终无需查找的资源   引用ID。例如,您希望/ 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

我希望这会对你有所帮助。