3

I have many controllers that will have some similar behavior, e.g. the user should be logged in, some scope needs to be set up, the current_account / current_user needs to be set and permissions cached.

I am think of using a standard controller and subclassing that.

class MyStandardController < ApplicationController
 before_filter :xyz
end 

class SomeController < MyStandardController
end

What I'm wondering is do I need to / when to call super at all?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jo Erlang
  • 327
  • 7
  • 15
  • I would also note the most of the actions you are referencing surround user authentication (are they them?) and authorization (do they have access to a specific resource?) that are best handled by gems like authlogic or devise with cancan. – Michael Durrant Mar 21 '12 at 15:19
  • I'm using devise, I may use cancan. – Jo Erlang Mar 21 '12 at 16:32

1 Answers1

5

You don't need to ever call super inside a controller that inherits from another controller; in fact, doing so would probably be kind of weird. Super executes a method of the same name from a superclass, and you probably won't have any methods on MyStandardController you'll redefine in its children.

The main reason to do this is, as you said yourself, to get filters and methods easily namespaced across controllers. We do something similar to this in our apps, where one area of the site with very similar behavior will inherit from a controller (like ShoppingController) that has a section of private convenience methods that are usable only across all its children.

However, realistically speaking, it would probably be better to have modules that implement the functionality you want, and include them in the controllers you want. Eventually you'll probably want something from one controller on another, and doing that is way easier with modules than with complicated inheritance hierarchies.

Veraticus
  • 15,944
  • 3
  • 41
  • 45
  • Totally agree with Veraticus. A simple solution would be to put the `before_filter` in `application_controller.rb` -- this is typical for cases like authentication. If there's really a namespace (a subset of the application's actions for which this applies, e.g. "Admin") then it's not a terrible idea to create a module containing the models (and views and controller) like `Admin::UserManager` -- the before_filter could then just be defined in `admin_controller.rb` (I think) – Tom Harrison Mar 21 '12 at 15:29
  • 1
    Thanks one other question, if I define a method, eg. mydef in MyStandardController, how can i make it available in my views? If I use helper_method to specify a helper in MyStandardController, it doesn't seem to available in view used by contollers that subclass that. – Jo Erlang Mar 21 '12 at 16:30
  • You probably shouldn't do that. Use a helper method instead, or call the method in your controller action and make the results available to your view via an instance variable. – Veraticus Mar 21 '12 at 16:33
  • Hmm, I don't know why subclasses wouldn't pick up ```helper_method```... sorry. :/ – Veraticus Mar 21 '12 at 16:34