Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top