简体   繁体   English

Rails路由/多态问题-如何为以下模型建模

[英]Rails routing/polymorphism issue - how to model the following

I have an app where a 'user' belong to a 'client' or a 'vendor' (and client and vendor has_many users). 我有一个应用程序,其中“用户”属于“客户端”或“供应商”(客户端和供应商has_many用户)。 In the admin namespace, I want to administer these users - so an admin would choose a client or a vendor, then nav to that client's or vendor's users. 在admin名称空间中,我要管理这些用户-管理员可以选择一个客户或一个供应商,然后导航到该客户或供应商的用户。 My question is, short of making the user model polymorphic, how could I model/route this? 我的问题是,除了不能使用户模型具有多态性外,我该如何对其建模/路由?

Here is what I have in terms of routing: 这是我在路由方面的优势:

map.namespace :admin do |admin|
  admin.resources :clients
  admin.resources :vendors
end

I know I could do something like: 我知道我可以做类似的事情:

map.namespace :admin do |admin|
  admin.resources :clients do |client|
    client.resources :users
  end
  admin.resources :vendors do |vendor|
    vendor.resources :users
  end
end

But the above would definitely need me to treat the User as polymorphic. 但是以上绝对需要我将用户视为多态的。

I'm just wondering what you would recommend or what my options are. 我只是想知道您会建议什么,或者我有什么选择。

Thanks. 谢谢。

I would try the second solution and construct your links like this: 我会尝试第二种解决方案,并像这样构建您的链接:

<%= link_to @vendor_or_client.name, [:admin, @vendor_or_client, @user] %>

Means: the magic comes from the Array syntax automatically. 意思是:魔术自动来自数组语法。 The same goes with render: 渲染也是如此:

<%= render [:admin, @vendor_or_client, @user] %>
<%= render [:admin, @vendor_or_client] %>

It will automatically render views/admin/users/_show.html.erb or views/admin/{vendors,clients}/_show.html.erb respectively. 它将分别自动呈现views/admin/users/_show.html.erbviews/admin/{vendors,clients}/_show.html.erb You can also use this Array syntax with forms etc. It will be pretty straight forward and you should have no problem with polymorphic routes. 您也可以将Array语法与表单等一起使用。这将非常简单,多态路由也应该没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM