Rails Many to Many Relationship with occasional Nested Routes
As I am learning more about rails, and breaking my design thinking from ASP.Net days, I was considering a scenario this morning but did not know if it was possible to do.
Practitioners have many Treatments through services - and vice versa
In my control Panel I have an area for Practitioners to edit their details (names, contact info etc) and can also choose their treatments via check-boxes.
I would like to remove the开发者_StackOverflow社区 check-boxes from the Practitioners _form. Having their own form which I could call on like this:
<%= link_to "Edit Treatments", edit_practitioner_treatments(@practitioner) %>
However, from an admin point of view I would still need to be able to manage treatments without a practitioner object in sight:
<%= link_to "Edit Treatments", edit_treatments(@treatment) %>
which also has authentication barriers.
- Is there an easier solution to extract treatments I have overlooked?
- Is it possible to have nested routes just some of the time?
- Did I have too much coffe this morning and are therefore currently in a state of insanity?
Generally when considering admin functions, which often present an entirely different interface to the user with permission checking based more on "will this break something" than "should you be allowed", it is advantageous to create an admin area with separate controllers. For example:
map.namespace :admin do |admin|
# Admin::PracticionersController
map.resources :practicioners
# Admin::TreatmentsController
map.resources :treatments
end
map.resources :practicioners do |practicioner|
practicioner.resources :treatments
end
map.resources :treatments do |treatment|
treatment.resources :practicioners
end
All Admin::* controllers can inherit from something such as Admin::BaseController that performs sufficiently rigorous authentication checking before allowing any actions to be performed.
In most applications I've seen, the user-facing front-end has an element of design or style to it, where they layout is often hemmed in by navigational elements, advertising, or other editorial content. Creating a separate admin view that's uncluttered, shows far more information per page, and allows sorting or searching on dimensions not exposed to the user is very valuable when managing even medium-sized sets of data.
Although it might seem like a lot of work to make these extra admin controllers and their associated forms, if you are careful in your design, you can recycle a lot of the functionality between the two areas, especially page partials.
精彩评论