Forum: Ruby on Rails require module in controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
john (Guest)
on 2007-03-26 11:19
(Received via mailing list)
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
Xavier N. (Guest)
on 2007-03-26 12:46
(Received via mailing list)
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
john (Guest)
on 2007-03-26 21:03
(Received via mailing list)
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.
Herryanto S. (Guest)
on 2007-03-27 05:16
(Received via mailing list)
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
This topic is locked and can not be replied to.