-1

To preface this, I am new to ruby and rails but I have done my homework and I really thought I was doing this right. So in my rails controller, I am loading another class called Input. I want to create an instance of the Input class and then call calc_value.

foo = Input.new "foo", 10, 120
render json: foo.calc_value

I know that all of the loading is setup correctly and I know that i can initialize foo correctly (I tried rendering the variable by itself without calling calc_value). But when I call calc_value I receive a "undefined method 'calc_value' for #".

Here is my Input class:

class Input
   def initialize (n = "", v = 0, m = 0)
      @name = n
      @value = v
      @maxValue = m
   end

   def calc_value
      @value
   end
end

I thought this would be simple and I have read everything over and over again about class variables and methods vs instance variables and methods. I am at my wits end here.

Pierre Tasci
  • 450
  • 2
  • 8
  • 4
    Nothing you've shown here would raise that error. Are you sure all of your work is saved? Is `Input` autoloaded? If not, have you tried restarting the server? – Zach Kemp Apr 17 '14 at 17:02
  • I feel both happy that I was doing things right and like a goof for the fix being that simple. I just needed to restart the server. If I could give you a million cookies right now I would. Thanks :) – Pierre Tasci Apr 17 '14 at 17:25
  • Thanks @PierreTasci. It's a good thing you can't give me a million cookies, because I would eat them. – Zach Kemp Apr 17 '14 at 17:52

2 Answers2

0

This seems like your rails app can't find the method where it thinks it should be.

Where is your class? The place to put user defined classes for Rails is your lib/ folder and called after loading using require. See this answer for the right type of setup.

Community
  • 1
  • 1
Mike
  • 346
  • 2
  • 8
0

The answer was simply that my local server needed a restart. Not sure why, but that fixed it.

Pierre Tasci
  • 450
  • 2
  • 8