14

This is probably a stupid question, but I'm new to Ruby on Rails and I could use a little guidance. I want to have a helper/utility class that performs a group of network operations and returns results. Where do I put that class and how do I use it.

I've created network_helper.rb in my app/modulename/helpers directory. In my controller when I try to do

  myNetworkHelper = ModuleName::NetworkHelper.new
  results = myNetworkHelper.getResults

I get an error

 undefined method `new' for MyModule::NetworkHelper:Module

I'm sure this is just a misunderstanding of how ruby on rails works. Can I get some clarification?

Would it be better to make this a class instead of a module and put it in libs? And can I add subfolders in libs and have them automatically loaded?

codeetcetera
  • 3,213
  • 4
  • 37
  • 62
  • 1
    It needs to be a class if you intend to use `new` on it. – Linuxios Mar 10 '13 at 03:56
  • I'm good with that. Added file `lib/utilities/network_utility.rb` I'm getting an error requiring that file. How does that require statement look for lib files? Also if you make this an answer I'll accept it. – codeetcetera Mar 10 '13 at 04:12

5 Answers5

8

Lib or Classes

Little utility classes like this typically go in the lib folder, though some people prefer to create a folder called classes. Whichever you choose, make sure you import the folder in config/application.rb, as the lib folder is not autoloaded:

config.autoload_paths += %W(#{config.root}/lib)

Concerns

If instead of a utility class, you want to extend some of your models with reusable code, you may also wish to look at the new Rails 4 concerns folders which encourage you to extract reusable modules:

see: How to use concerns in Rails 4

Community
  • 1
  • 1
superluminary
  • 47,086
  • 25
  • 151
  • 148
4

To use new, the thing your calling it on must be a class, not a module. You're using a module. Change module to class in lib/utilities/network_utility.rb.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • How do I include this in my controller in a ruby friendly manner? – codeetcetera Mar 10 '13 at 04:26
  • 2
    @btate: Using `require_relative`. If your main file is in, say, `bin/main.rb` (whatever), use `require_relative '../lib/utilities/network_utility.rb'`. – Linuxios Mar 10 '13 at 04:27
  • Strictly speaking, you *can* call `new` on a Module, you’d just have to define it. Though it probably wouldn’t do what you want. Nothing particularly special about `new`. – Andrew Marshall Mar 10 '13 at 04:40
  • @AndrewMarshall: True. But once you do that, well, your just going to confuse people. – Linuxios Mar 10 '13 at 14:10
1

I cannot verify this at the moment, however I believe one place you can store your custom modules and classes is the lib directory. Alternatively, you should be able to store them in the app directory in the manner you have indicated by adding the following line to your environment.rb:

config.load_paths << File.join(Rails.root, "app", "modulename")

Also, check out Yehuda Katz's answer, which I think not only answers your question better, but also contains some very interesting and useful information and concepts relating to your situation. Hope that helps!

Community
  • 1
  • 1
Paul Richter
  • 10,908
  • 10
  • 52
  • 85
1

In a Ruby on Rails application, the "utils" and "helper" folders are two different ways of organizing shared code in your application.

The "utils" folder typically contains standalone methods or functions that can be used across different parts of the application. These methods are often general-purpose, such as data manipulation, string formatting, or math operations. The "utils" folder is not tied to any specific part of the application and can be used by any other module or component.

On the other hand, the "helpers" folder typically contains modules that are used to provide methods that are specific to a particular part of the application, such as a view or a controller. For example, a view helper might provide a method for formatting dates or generating links, while a controller helper might provide methods for handling authentication or authorization.

0

Add your class to app/lib folder instead of lib, so that you don't change autoload paths!

Explanations:

The accepted answer suggests adding the classes to lib.

But according to this discussion:

The lib folder does not belong to the autoload paths since Rails 3.

So it's discouraged to add lib under autoload path. Use app/lib instead.

user10375
  • 613
  • 1
  • 6
  • 11