2016-09-26 19 views
1

當我運行在軌道傾角路線5應用程序,我看到可用以下網址:支架航線不希望有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 
當我創建的link_to資源我需要包括ID PARAM在

這個whay它喜歡:

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

代替軌道4路:

link_to article.name, manager_articles_path(article) 

我怎樣才能使軌道5條航線表現爲軌道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 
+0

嘿請爲您的文章顯示您的'routes.rb'代碼。 –

+0

@ABPrime更新了帖子。 – rmagnum2002

回答

5

在軌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 

我希望這會幫助你。

+0

哦,facepalm,我的'.. :)謝謝。 – rmagnum2002

+0

@ rmagnum2002:發生了。 –