I have a Rails application called RailsApp. In my RailsApp/lib directory, I have a file called localization.rb that defines a module called RailsApp with a Localization class that implements a method l. (RForum devs and fans will recognize this as the YAML-based localization feature in that package.) I want to be able to call this method from my controllers and views. I have added a ''require localization'' to my application.rb, plus an include RailsApp::Localization in application.rb, my controller, and my model. I can use the l method from my controller using @title = l(:test), but not from my view using <%=l :test %>. The latter generates a NoMethodError. Where can I "requrie" this to be available in my views? I grepped the RForum source and can''t figure it out. To describe the problem in another way (so that hopefully others will be able to google the solution for years to come): I want a custom lib method to be available in my view rhtml so that Rails doesn''t produce a NoMethodError. Thanks Matt
Matt, To make module methods available to views you need to use the ''helper'' method. So in the controller (application.rb for all) include the following line: helper :localization This will default to looking for a LocalizationHelper in the app/helper folder and require it in the controller and include it in the template. This is used for helpers in the app/helpers folder generally. For libs... You can do: require ''localization'' helper LocalizationHelper This should work without copying anything, but don''t trust me on it... Thanks, Chris> -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails- > bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Ma Lu > Sent: Tuesday, March 08, 2005 3:53 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] Making a module method available in view > > I have a Rails application called RailsApp. In my RailsApp/lib > directory, I have a file called localization.rb that defines a module > called RailsApp with a Localization class that implements a method l. > (RForum devs and fans will recognize this as the YAML-based > localization feature in that package.) > > I want to be able to call this method from my controllers and views. > > I have added a ''require localization'' to my application.rb, plus an > include RailsApp::Localization in application.rb, my controller, and > my model. > > I can use the l method from my controller using @title = l(:test), but > not from my view using <%=l :test %>. The latter generates a > NoMethodError. Where can I "requrie" this to be available in my > views? I grepped the RForum source and can''t figure it out. > > To describe the problem in another way (so that hopefully others will > be able to google the solution for years to come): I want a custom > lib method to be available in my view rhtml so that Rails doesn''t > produce a NoMethodError. > > Thanks > > Matt > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks for the reply. I tried what you suggested, i.e. putting a helper method in my application controller, but I am now getting a NameError: uninitialized constant LocalizationHelper. What I deserved instead of a speedy reply was list etiquette lecture for not mentioning that I am running Rails 10.0 on Ruby 1.8.2 and not including code snippets. Here they are: #RailsApp/lib/localization.rb: module RailsApp; module Localization def l(symbol, *arguments) ... end ... end;end #RailsApp/app/controllers/application.rb: require ''localization'' class ApplicationController < ActionController::Base include RailsApp::Localization helper LocalizationHelper end #RailsApp/app/controllers/users_controller.rb: ... include RailsApp::Localization ... def new @title = l(:test) # this returns the expected result @users = Users.new @clients = Client.find_all end ... #RailsApp/app/views/users/new.rhtml ... <%=l :test %> # throws the nomethoderror ... Thanks for taking another look at it! Matt On Tue, 8 Mar 2005 16:58:05 -0500, Williams, Chris <Chris.Williams-yLTBD9Em4aGakBO8gow8eQ@public.gmane.org> wrote:> Matt, > To make module methods available to views you need to use the > ''helper'' method. > > So in the controller (application.rb for all) include the following > line: > > helper :localization > > This will default to looking for a LocalizationHelper in the app/helper > folder and require it in the controller and include it in the template. > This is used for helpers in the app/helpers folder generally. For > libs... > > You can do: > require ''localization'' > helper LocalizationHelper > > This should work without copying anything, but don''t trust me on it... > > Thanks, > Chris > > > -----Original Message----- > > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails- > > bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Ma Lu > > Sent: Tuesday, March 08, 2005 3:53 PM > > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > Subject: [Rails] Making a module method available in view > > > > I have a Rails application called RailsApp. In my RailsApp/lib > > directory, I have a file called localization.rb that defines a module > > called RailsApp with a Localization class that implements a method l. > > (RForum devs and fans will recognize this as the YAML-based > > localization feature in that package.) > > > > I want to be able to call this method from my controllers and views. > > > > I have added a ''require localization'' to my application.rb, plus an > > include RailsApp::Localization in application.rb, my controller, and > > my model. > > > > I can use the l method from my controller using @title = l(:test), but > > not from my view using <%=l :test %>. The latter generates a > > NoMethodError. Where can I "requrie" this to be available in my > > views? I grepped the RForum source and can''t figure it out. > > > > To describe the problem in another way (so that hopefully others will > > be able to google the solution for years to come): I want a custom > > lib method to be available in my view rhtml so that Rails doesn''t > > produce a NoMethodError. > > > > Thanks > > > > Matt > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks entirely to Chris''s advice, I''ve managed to get it working. I removed the "wrapper" module RailsApp so that my localization.rb looked like: module Localization ... end and removed all of my references to RailsApp::Localization. Then I added to my application.rb: include Localization helper Localization (no "require" because my RailsApp/lib/localization.rb is in Rails'' loadpath anyway) and that''s it, really. the "l" method is available to all of my controllers and views. Thanks all! BTW - anyone looking for a relatively painless way to handle localization on rails would do well to check out RFourm''s lib/localization.rb. It uses flat YAML files to store localized labels, which I assume means that unicode is off limits. I don''t even plan to use anything but an en-us localization, but I wanted to build in support for localized labels, buttons, messages, etc from the ground up. I may hack another method into the lib to support localized icons/images (en_reply.gif vs de_reply.gif). If I do, I''ll make it available somewhere. Matt On Tue, 8 Mar 2005 17:29:15 -0500, Ma Lu <mattlrb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for the reply. I tried what you suggested, i.e. putting a > helper method in my application controller, but I am now getting a > NameError: uninitialized constant LocalizationHelper. > > What I deserved instead of a speedy reply was list etiquette lecture > for not mentioning that I am running Rails 10.0 on Ruby 1.8.2 and not > including code snippets. Here they are: > > #RailsApp/lib/localization.rb: > > module > RailsApp; module Localization > > def l(symbol, *arguments) > ... > end > > ... > > end;end > > #RailsApp/app/controllers/application.rb: > > require ''localization'' > > class ApplicationController < ActionController::Base > > include RailsApp::Localization > helper LocalizationHelper > > end > > #RailsApp/app/controllers/users_controller.rb: > > ... > include RailsApp::Localization > > ... > def new > @title = l(:test) # this returns the expected result > @users = Users.new > @clients = Client.find_all > end > ... > > #RailsApp/app/views/users/new.rhtml > ... > <%=l :test %> # throws the nomethoderror > ... > > Thanks for taking another look at it! > > Matt > > > On Tue, 8 Mar 2005 16:58:05 -0500, Williams, Chris > <Chris.Williams-yLTBD9Em4aGakBO8gow8eQ@public.gmane.org> wrote: > > Matt, > > To make module methods available to views you need to use the > > ''helper'' method. > > > > So in the controller (application.rb for all) include the following > > line: > > > > helper :localization > > > > This will default to looking for a LocalizationHelper in the app/helper > > folder and require it in the controller and include it in the template. > > This is used for helpers in the app/helpers folder generally. For > > libs... > > > > You can do: > > require ''localization'' > > helper LocalizationHelper > > > > This should work without copying anything, but don''t trust me on it... > > > > Thanks, > > Chris > > > > > -----Original Message----- > > > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails- > > > bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Ma Lu > > > Sent: Tuesday, March 08, 2005 3:53 PM > > > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > Subject: [Rails] Making a module method available in view > > > > > > I have a Rails application called RailsApp. In my RailsApp/lib > > > directory, I have a file called localization.rb that defines a module > > > called RailsApp with a Localization class that implements a method l. > > > (RForum devs and fans will recognize this as the YAML-based > > > localization feature in that package.) > > > > > > I want to be able to call this method from my controllers and views. > > > > > > I have added a ''require localization'' to my application.rb, plus an > > > include RailsApp::Localization in application.rb, my controller, and > > > my model. > > > > > > I can use the l method from my controller using @title = l(:test), but > > > not from my view using <%=l :test %>. The latter generates a > > > NoMethodError. Where can I "requrie" this to be available in my > > > views? I grepped the RForum source and can''t figure it out. > > > > > > To describe the problem in another way (so that hopefully others will > > > be able to google the solution for years to come): I want a custom > > > lib method to be available in my view rhtml so that Rails doesn''t > > > produce a NoMethodError. > > > > > > Thanks > > > > > > Matt > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >