Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 998
TMB Avatar asked Apr 24 '09 15:04

TMB


2 Answers

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
like image 91
Matt Grande Avatar answered Oct 28 '22 09:10

Matt Grande


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.

like image 45
accuser Avatar answered Oct 28 '22 07:10

accuser