Search code examples
ruby-on-railsdrycontrollers

Rails - Two controllers or adding actions?


Designing a web app with a admin section and a public facing section. It feels like having a public facing controller just for "index" and "show" is a bit redundant. All the suggestions I've read suggest a namespace for admin, which is fine. I just wonder if I should have one controller with an addition action, say "list_public" or something like that.

I'm new with Rails, so maybe I'm just concerned about nothing. I just don't like the idea of having all these controllers, views, helpers with the same name scattered all over my project directories.

Anyone have any insight to this? Thanks in advance.


Solution

  • I would say having both controllers (one public, and one admin) is the best solution.

    Now what you could do is have both the controllers call the same method that does the related actions in the actions.

    class MyController < ApplicationController
      def show
        MyModel.do_all_sorts_of_stuff
      end
    end
    
    class Admin::MyController < ApplicationController
      def show
        MyModel.do_all_sorts_of_stuff
        # Admin only stuff goes here
      end
    end