views:

138

answers:

2

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.

+7  A: 

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
Matt Grande
Thanks, Matt. This makes sense I just need some verification that it's not "overdone."
TMB
+3  A: 

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 just focus on the more specialised cases for Admin::MyController rather than repeating code.

accuser