Topic: How to deal with redundant actions in controllers?

I have a "login" controller which includes an "add_user" action
I also have an "admin" controller -- meant to be used by system administrators only -- which also includes an "add_user" action

Both of the add_user actions share a lot of common code, but have a few important differences (they require different types of authentication to access, when an admin wants to add a user it doesn't require as many confirmation steps, and the layouts for the two are different). 

Ideally I would like to not repeat myself so much.  What is the best way to deal with this?  I was thinking perhaps of putting part of the code into the application controller, but I'm not sure if that's entirely appropriate, and I'm not completely sure how I should be interacting with the application controller.

Would be great to get some advice on this..  Thanks!

Re: How to deal with redundant actions in controllers?

I prefer to keep the public controller and the admin controller one and the same to avoid this duplication. See this thread for details.

You then handle the differences with simple if conditions. For example, to render a different layout for the admin you can do this:

# in ApplicationController or any other controller
layout :user_layout

#...

def user_layout
  if current_user.admin?
    "admin"
  else
    "application"
  end
end


This helps remove the duplication.

If you don't want to take this approach, another way to solve the problem is to extract the duplicate code into a method in the controller. Then move this method into either the ApplicationController or a module.

Railscasts - Free Ruby on Rails Screencasts