2016-08-10 100 views
0

我有一些定義路線如下:Rails的路由 - 添加參數:資源

namespace :owners do 
    resources :orders, only: [:show, :edit, :update] do    

    resources :bibles, only: [:update] 
    end 
end 

它創建所有你所期望的不錯途徑。有一點需要注意:我想一個額外的參數添加到edit路線的終點,把這個

/owners/orders/:id/edit 

/owners/orders/:id/edit/:another_parameter 

有什麼Railsy辦法做到這一點?

+0

:id/edit route意味着你正在編輯具有給定id的實體。你提出的不是RESTful,所以沒有真正的官方方式來做到這一點。也許你可以添加更多你想要做的解釋,爲什麼你需要這個參數? – apchester

+0

附加參數主要作爲版本號。 – opticon

+0

我認爲你可能需要在這裏的新模型來表示每個訂單的版本。這樣做,你會得到更多的傳統路線沿着/所有者/訂單/:order_id/versions /:id /編輯這是更符合RESTful原則。 – apchester

回答

0

你可以做到這一點,以達到你想要

namespace :owners do 
    resources :orders, only: [:show, :update] do 
    member do 
     get 'edit/:another_parameter', to: 'orders#edit' 
    end 
    resources :bibles, only: [:update] 
    end 
end 

這會給你下面的路線是什麼:

    GET /owners/orders/:id/edit/:another_parameter(.:format) owners/orders#edit 
owners_order_bible PATCH /owners/orders/:order_id/bibles/:id(.:format)  owners/bibles#update 
        PUT /owners/orders/:order_id/bibles/:id(.:format)  owners/bibles#update 
     owners_order GET /owners/orders/:id(.:format)       owners/orders#show 
        PATCH /owners/orders/:id(.:format)       owners/orders#update 
        PUT /owners/orders/:id(.:format)       owners/orders#update 
0

我不認爲你有,因爲你的手路由問題你呼叫總是會去到同一個控制器。

我想你想要的是你做這樣的事情傳遞給控制器​​一個額外的參數:

edit_owners_orders_path(@order, another_parameter: 'foo') 

這應該產生這樣的:

/owners/orders/:id/edit?another_parameter=foo 

然後,在你的控制器,你應該得到如下參數:

{id: 1, another_parameter: 'foo'} 

某些語法可能不會b e完全正確。爲此道歉。但我相信這是一個可行的方向。