views:

190

answers:

1

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 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?
+1  A: 

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.

tadman
wow, that's a great way of splitting the two areas, love it! My method of thinking is obviously not yet in line with ror's idioms - where did you learn your design methods, or did you just work it out from experience? Thanks again for helping - a massive help for me!
Joe
This is something that's sort of the result of building a dozen or so serious Rails sites. You see patterns like this. Hope it works out for you, too.
tadman