Require module in controller


#1

I have some calculations in a module that is under the /lib folder -
but when using require the action of the module is not found in the
controller. Does anyone have an example of the correct syntax to get
this working. Thanks


#2

On Mar 26, 2007, at 9:19 AM, john wrote:

I have some calculations in a module that is under the /lib folder -
but when using require the action of the module is not found in the
controller. Does anyone have an example of the correct syntax to get
this working.

It should work, and if the name of the file follows Rails conventions
you don’t even need an explicit require.

You may be overlooking something in your very Ruby code, like
forgetting to mixin the module in the controller, or trying to use a
module method that is actually defined as an instance method.

– fxn


#3

yeah, I know I’m missing something, that is why I was hoping for a
brief code example (ie I’m not sure what is the “Rails conventions” on
this.


#4

Hi John,

Did you miss out any of the steps below?

calculator.rb

module Calculator
def sum(a,b)
a+b
end
end

config/environment.rb or test_controller.rb

require ‘calculator’

test_controller.rb

class TestController < ActiveRecord::Base
include Calculator

def test_sum
# you should be able to call sum here
@sum_value = sum(1,4)
end
end

Herry