Hello, 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? Many thanks, Ryan Glover -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Ryan, Ryan Glover wrote:> 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.Just put your methods in application.rb. Any method there is available to all controllers in the app. hth, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/11/07, Ryan Glover <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hello, > > 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 Chad --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. Any thoughts? Many thanks, Ryan Glover -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/11/07, Ryan Glover <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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. > > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Whoops, you are correct. Unfortunately, that is a typo from me trying to simplify the code for the forum post and that error is not in my actual code. Thanks anyway, Rg -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. Rg -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---