14

In Rails, supposing that the file is already loaded, how it is possible to call my_method from this example from console?

# some_file.rb
class MyClass < ApplicationController::Base
  def my_method(args)
Martin
  • 11,216
  • 23
  • 83
  • 140
  • That actually sounds interesting.But my first thought would be that all methods inside controllers are accessed by the views.When you access the console, you're accessing ActiveRecord and get access to your models and their methods.As I said,that would be my first thought. – Daniel May 20 '12 at 05:22
  • http://stackoverflow.com/q/151030/2355112 has really good answers regarding the same issue. – oozzal Sep 15 '14 at 05:00

3 Answers3

32

Another, very simple way to do this is to use an instance of ApplicationController itself.

ApplicationController < ActionController::Base
  def example
    "O HAI"
  end
end

Then in the console, you can do the following:

>> ApplicationController.new.example

This will output the following:

O HAI

This, of course, has the restriction of not having access to everything a normal request would, such as the request object itself. If you need this, as the Patrick Klingemann suggested, you could use the debugger... I personally recommend using Pry:

This is likely much too late for you, but hopefully it will help someone in the future.

slant
  • 905
  • 8
  • 15
3

use debugger:

in your Gemfile add:

gem 'debugger'

then from the terminal:

> bundle
> rails s --debugger

in the controller action you're hitting:

class WidgetsController < ApplicationController
  def index
    debugger
    @widgets = Widget.all
    respond_with @widgets
  end
end

then point your browser to: http://localhost:3000/widgets, the page will not finish loading. Return to the terminal where your server is running and you'll be in an interactive debugging session where you can run: my_method

Patrick Klingemann
  • 8,884
  • 4
  • 44
  • 51
  • I actually forgot about debugging.Still notice that you won't get access to the method launching your server like that. You'd need to: rails s --debugger.And even in that case the only response you get is "Just hit an ApplicationController method"... no actual output – Daniel May 20 '12 at 05:45
  • I thought of the --debugger as I went to sleep, good point. I'm not sure I understand the second sentence in your comment. – Patrick Klingemann May 20 '12 at 13:31
  • well I tried reproducing the case Martin set up for us but what I get is that message...I mean I can't actually check what's going on inside the method when I call it.I might be doing something wrong though... – Daniel May 20 '12 at 14:01
  • (Displaimer: This is only my opinion.) debugger is dead, man. Give [pry](https://github.com/pry/pry) a shot! – slant Oct 31 '12 at 02:51
1

It is not exactly the question asked, but you can also debug with the pry gem, similarly to debugger.

Add to the Gemfile:

gem "pry"
gem "pry-remote"
gem "pry-stack_explorer"
gem "pry-debugger"

In your method:

def myMethod
  binding.pry
  # some code
end

Done!

When you run your method, the page processing will freeze at binding.pry and pry will take over the prompt. Type n for each new step of the method, and play around with your variables that can be print (just typing them) in "real-time"!

Teo Inke
  • 5,928
  • 4
  • 38
  • 37