Share code between "normal" controller and "admin" controller - best practice?

Hi, How do you share common logic between an “admin” controller and a “normal” controller.

I have this scenario where I have an

class Organization < ApplicationRecord
   name, description, published_at
   belongs_to :user

A user can create and manage their organizations. But only and administrator can publish an organization. Also an administrator can see all the organizations, from all the users, even the unpublished ones

I have

class OrganizationsController < ApplicationController
# accepts only name and description for create/update and allows index/show/destroy only for the organizations of the user 
class Admin::OrganizationsController < ApplicationController
# accepts published_at from the form and allows create/read/update/delete of all the organizations. 

Naturally there is some common logic between the controllers. Like the list of accepted params. The create method is the same. The update is the same. But they redirect to different places on success:

  • OrganizationsController redirects to organizations_path
  • Admin::OrganizatiosController redirects to admin_organizations_path

What do you guys do?

  1. Extend - eg Admin::OrganizationsController < OrganizationsController
  2. Have a common concern?
  3. Implement the two controllers completely separately?
  4. Have one controller that manages both admin and non-admin logic?
  5. Something else.


1 Like

I’ve had the same thing between “api” controllers and server-side rendered “ui” controllers. I found concerns to be a fairly good solution. Common behavior is in the concern, differences in the specific controllers.


If it’s just two cases, I’d keep them separate and duplicate everything. Too early to tell whether they should be sharing any abstractions.

Would you reuse the views or you would implement separate views?

That’s up to whether it makes sense for your particular use case. The admin views I see usually aren’t similar enough to user views to warrant reuse.

If you are using a concern as suggested above, then I’ve reused views by keeping them in views/concerns/ folder.