简体   繁体   中英

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). 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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