I have a group of methods that I use across numerous controllers. I
want to put the methods in their own file and call them in from the
controllers. My (limited) understanding is that modules are the
solution to this problem. The methods could be considered class
methods. For example, there is a method that accepts a string and
returns the count of each character (a, b, c, etc).
What is the best way to make these methods available to all the
controllers that might want to use them? In Objective-C (my other
exposure to MVC) I would create a class of the methods, include it and
call the class methods. What is the corresponding approach in RoR?
I have a group of methods that I use across numerous controllers. I
want to put the methods in their own file and call them in from the
controllers. My (limited) understanding is that modules are the
solution to this problem. The methods could be considered class
methods. For example, there is a method that accepts a string and
returns the count of each character (a, b, c, etc).
Modules are indeed a good way to do this. You could, as one poster
suggested, throw them all into application.rb. But you might find
that file getting big and unwieldy. An alternative is to group them
into a well-named module or set of modules and then to include those
modules in any controllers (or any other context) in which they’re
needed. You could put the module in a file in, say, your lib/
directory, require that file in environment.rb and then where ever it
makes sense, do:
class MyController < ApplicationController
include MyWellNamedModule
def some_action
some_method_which_counts_characters_in_a_string(“a string”)
end
end
Thank you for the suggestions. I have implemented the following module
approach but it keeps raising an error. I followed the example in the
pick axe book.
I have a module named reading.rb in my /lib directory. It contains the
following (modified example to keep things simple, it still doesnt
work).
module Reading
def Reading.text_length(text)
length = text.length
return length
end
end
Then, I have a controller class with the following code.
require ‘reading’
class ProcessController < ApplicationController
def analyze
submission = params[:submission] @text = submission[:text] @text_length = Reading.reading(@text)
end
end
It doesn’t use the include statement as I am following the first example
in the module chapter of the pick axe book.
However, when I run this, I get the following error.
undefined method `reading’ for Reading:Module
If I take the method from the module and put it right in the controller
(and change the call from Reading.reading to just reading), it works
great.
I solved the issue. As you can see in my above example, I was using
length as a variable name in the module. As soon as I changed that to
something else it worked great. Sorry for wasting everyones time.
If I take the method from the module and put it right in the controller
(and change the call from Reading.reading to just reading), it works
great.
Any thoughts?
You called the method something different from the name you’re using
to call it. I think you just made a simple mistake. Look closely at
the definition vs. what you’re calling in the controller
Chad
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.