2016-08-10 103 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完全正确。为此道歉。但我相信这是一个可行的方向。