【发布时间】:2020-05-07 14:28:24
【问题描述】:
我已经研究过这个主题,但只找到了部分解决方案。这里更好地解释了这个问题:
我管理 2 类业务对象:
- 定义的业务对象,可以认为是模板(有一个标志is_template=true)
- 使用的业务对象,是以前的副本,已实现,有细微差别
我对路由有 3 个期望:
依赖辅助工具,例如 used_business_objects_path 或 new_defined_business_object_path
显示用户友好的 URL,例如 /used_metadata/new(metatada 是我的客户要求显示的名称,而不是 Business Object)
传播控制器使用的类型变量
我想为这些人构建一个资源丰富的路线,包括控制器的参数,他应该选择哪种类型的对象:
# GET /business_objects
# GET /business_objects.json
def index
if params[:type] == 'defined'
@business_objects = BusinessObject.joins(translated_objects).pgnd(current_playground).defined.visible.search(params[:criteria]).
select(index_fields).order(order_by).paginate(page: params[:page], :per_page => paginate_lines)
else
@business_objects = BusinessObject.joins(translated_objects).pgnd(current_playground).used.visible.search(params[:criteria]).
select(index_fields).order(order_by).paginate(page: params[:page], :per_page => paginate_lines)
end
根据nick garrett 的建议,我实现了一个范围路由:
scope :type, constraints: { :type => /(defined|used)/ } do
resources :business_objects, :path => "#{:type}_metadata" do
resources :skills
resources :skills_imports, :only=>[:new, :create]
resources :scopes
member do
post :new_version
post :make_current
post :finalise
post :activate
post :open_cart # Declares that the current business object collects skills as a cart
post :close_cart # Unsets the current business as cart
get :derive
end
collection do
get :index_all
end
end
end
不幸的是,这会生成一个 URL 为 /type/type_metadata?type=used 并且助手似乎不将类型视为参数。
阅读 Rails 路由指南后,我不确定我尝试实现的目标是否可行。您能帮忙找到解决方案吗? 非常感谢!
【问题讨论】:
标签: ruby-on-rails routes