list Rails controller instance variables
Asked Answered
S

2

12

i was trying to list the instance variables inside a controller but came up with

irb>HomeController.instance_variable_names
=> ["@visible_actions", "@inheritable_attributes", "@controller_path", "@action_methods", "@_process_action_callbacks"]

and I tried it on the action

irb>HomeController.action("index").instance_variable_names
=> []

so what do controller Instance variables belong to?

Silden answered 19/5, 2011 at 16:2 Comment(0)
O
13

The instance variables belong to the instantiated controller object, and are only created when the action method has executed. Try this:

irb>instantiated_controller = HomeController.new
irb>instantiated_controller.index
irb>instantiated_controller.instance_variable_names
=> ["@_status", "@_headers", ...
Obellia answered 19/5, 2011 at 16:20 Comment(0)
I
2

You can also call self.instance_variable_names directly from the controller code and then see them in logs.

class ProfilesController < ApplicationController
  ...
  def update
    logger.info("List of instance vars: #{self.instance_variable_names}")
    ...
  end
end
Interpellate answered 6/4, 2020 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.