![](/img/trans.png)
[英]How to define restfull routes to seperate 2 categories of 1 class with Rails 5.2?
[英]How to define 2 routes to seperate 2 categories of 1 object in Ruby on Rails 5.2?
我的應用程序管理業務對象。 我有 2 類業務對象:
它們是相同的,但“已實現”類別成員始終引用“模板”,並且有一些字段在編輯視圖中顯示為只讀。 所以我只有 1 個班級和 1 個控制器。 “is_template”標志標記模板業務對象。
現在一個新的要求要求在路由級別將這 2 個類別分開。 在一條路徑上,只能管理“模板”業務對象,而在另一條路徑上,可以管理“已實現”的業務對象。 兩者都通過同一個控制器。 我嘗試了以下方法,但不知道如何處理:
resources :business_objects, :path => "template_metadata" do
resources :skills # Attributes of a business object
member do
post :new_version # Manage some flags for templates
post :make_current
post :finalise
end
collection do
get :index_used # Former way to list only implemented business objects
end
end
resources :implemented_objects, controller: 'business_objects' do
resources :skills # Attributes of a business object
end
感謝您的幫助!
似乎您需要在路由中添加一個額外的參數,以便您可以檢查控制器中的對象類型。 如果您將路由包裝在具有命名參數的作用域中,它可以將其傳遞給您的控制器。 例如,
scope ":type", constraints: { :type => /(template|implemented)/ } do
resources :business_objects, :path => "template_metadata" do
resources :skills # Attributes of a business object
member do
post :new_version # Manage some flags for templates
post :make_current
post :finalise
end
collection do
get :index_used # Former way to list only implemented business objects
end
end
resources :implemented_objects, controller: 'business_objects' do
resources :skills # Attributes of a business object
end
end
將強制所有這些路由以type
為前綴,您將能夠訪問控制器中的params[:type]
。 constraints: { :type => /(template|implemented)/ }
部分確保唯一的類型是template
或implemented
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.