Rails - Two controllers or adding actions?

Development of a web application with an administrator section and a shared section. It seems that having a public controller just for the “index” and “show” is a little redundant. All the suggestions that I read offer a namespace for the admin, and that's fine. I'm just wondering if I should have one controller with an extra action, say "list_public" or something like that.

I'm new to Rails, so maybe nothing just bothers me. I just don't like the idea of ​​having all of these controllers, views, helpers of the same name scattered across all of my project directories.

Does anyone have an understanding of this? Thanks in advance.

+5
source share
2 answers

I would say that the best solution is to use both controllers (one public and one administrator).

Now, what you could do is force both controllers to call the same method that performs 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
+7
source

As Matt said, but you can also do this:

class MyController < ApplicationController
  def show
    MyModel.do_all_sorts_of_stuff
  end
end

class Admin::MyController < MyController
  def show
    super
    # Admin only stuff goes here
  end
end

This means that you can simply focus on more specialized cases for Admin :: MyController, rather than on code repetition.

+6
source

All Articles