list Rails controller instance variables

后端 未结 2 1649
不知归路
不知归路 2021-02-20 05:48

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

irb>HomeController.instance_variable_names
=> [\"@visible_actions\", \"@in         


        
相关标签:
2条回答
  • 2021-02-20 05:57

    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
    
    0 讨论(0)
  • 2021-02-20 06:00

    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", ...
    
    0 讨论(0)
提交回复
热议问题