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开发者_如何学JAVA :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.
精彩评论