2016-12-07 56 views
1

創建內部資源的自定義路線我已經在這個routes.rb中如何在沒有覆蓋路徑

resources :questions, except: [:show] do 
    get '/resource/:subject/:id', to: 'resource#show', as: "resource", param: [:name, :id] 

它說:

無效的路由名稱,已在使用:「資源」你可能已經使用:as選項定義的兩條路線具有相同的名稱,或者您可能會改寫命名已經被具有相同的資源定義的路由

我知道資源創建兩條具有相同路徑的路線,showdestroy都使用resource_path,它是如何在內部創建的?以及我如何才能生成我的演出路線而無需覆蓋摧毀中的演出路線?

回答

2

消除路線的一個好方法是不需要通過指定:唯一的選擇

resources :user, :only => [:edit] 

,而不是

resources :user, :except => [:new, :create, :edit, :update, :show, :destroy] 
0

在我看來,你可以拿出秀,然後定義你想分開的路線。看看這個工程:

resources :questions, except: :show 

get '/resource/:subject/:id', 
    to: 'resource#show', 
    as: "resource", # This is where the error is. 
    param: [:name, :id] 

編輯:嗯,是的。 :as參數需要一個不同的名稱。這將工作:

resources :questions, except: :show 

get '/resource/:subject/:id', 
    to: 'resource#show', 
    as: "resource_show", 
    param: [:name, :id]