Forum: Ruby on Rails Controller methods

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
kevin.jackson (Guest)
on 2005-12-09 10:10
(Received via mailing list)

I want to be a little clever with my views and basically inspect a
controller to autodiscover it's methods and then create a link for each

Now I've given a try to, but I get a problem
with the dependency on ApplicationController (in the console), is there
a way to do this?  I assume there is a way to do this.

What I want is

def available
  @available = ReportsController.actions - [:index, :list]

or something similar

Jamie M. (Guest)
on 2005-12-09 21:56
(Received via mailing list)
There are two methods that do what you're looking for.

Object.methods returns a list of all the class methods,
Module.instance_methods returns a list of all the instance methods.

What you're looking for are the instance methods of your controller,
without anything that belong to the superclass (ApplicationController).

As such, try this (you might be able to stick it in
ApplicationController and still work, haven't actually tried it):

def actions
  # start with all my methods
  self.class.instance_methods -
    # remove my superclass's methods
    self.class.superclass.instance_methods -
    # manually remove anything else I don't want showing up

def non_action_methods
  # this method should be overridden by the concrete controllers,
  # and list anything you don't want showing up.
  # Also good to list any methods of mixins, like a login generator
  ['index', 'list']

Do note that the method names returned from non_action_methods should be
strings, not symbols.

- Jamie
This topic is locked and can not be replied to.