Forum: Ruby on Rails Controller methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 kevin.jackson (Guest)
on 2005-12-09 09:10
(Received via mailing list)
Hi,

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
one.

Now I've given a try to MyController.new.methods, 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]
end

or something similar

Kev
Eb234d1ee9f1dcd334657d7c5c1b1c4d?d=identicon&s=25 Jamie Macey (jamie)
on 2005-12-09 20: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
    non_action_methods
end

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']
end

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.