Cross-posting from comp.lang.ruby Hi, I am quite new to ruby and ruby on rails. I am trying to user helper module in my controller but fail :( I added languages_helper.rb file to helpers directory in my RoR app. The file looks like this: module LanguagesHelper def LanguagesHelper.sort_to_first(langs, lang) ...my secret method here... end end Then I want to use it in LanguagesController. I put require "languages_helper" right in the beginning of the class body, and then in one of controller''s methods I try to do ordered_langs = LanguagesHelper.sort_to_first(all_langs, lang) When while browsing I come to this place in my browser, there is a message: undefined method `sort_to_first'' for LanguagesHelper:Module What is wrong? Best regards Pawel Stawicki -- 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 -~----------~----~----~----~------~----~------~--~---
> require "languages_helper" > ordered_langs = LanguagesHelper.sort_to_first(all_langs, lang) > undefined method `sort_to_first'' for LanguagesHelper:Modulei believe the error appears because the LanguageHelper isn''t included correctly and so the method isn''t actually embedded in the controller; did you add ''include LanguagesHelper'' ? (this mixes it in) also, you can put in modules in the RAILS_ROOT/lib folder and require them just as easily (it doesn''t have to be a ''helper'') -- 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 believe the error appears because the LanguageHelper isn''t included > correctly and so the method isn''t actually embedded in the controller;I think it is included correctly, because in error message there is undefined method, but there is nothing like "unknown module LanguagesHelper". I think module is available, but method is not. (But of course I can be wrong).> did you add > > ''include LanguagesHelper'' ? (this mixes it in)I tried this too, but it didn''t help :(> also, you can put in modules in the RAILS_ROOT/lib folder and require > them just as easily (it doesn''t have to be a ''helper'')I prefer to have it in helper, just for this app. Thanks anyway Regards Paweł Stawicki -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
>> i believe the error appears because the LanguageHelper isn''t included >> correctly and so the method isn''t actually embedded in the controller; > > I think it is included correctly, because in error message there is > undefined method, but there is nothing like "unknown module > LanguagesHelper". I think module is available, but method is not. (But > of course I can be wrong)...i''m not sure about this, but if you change module LanguagesHelper def LanguagesHelper.sort_to_first(langs, lang) ...my secret method here... end end to module LanguagesHelper def sort_to_first(langs, lang) ...my secret method here... end end and then in the controller, just put the method name (without prepending it to LanguageHelper) like ordered_langs = sort_to_first(all_langs, lang) i''ve had something similar, and i believe this worked for me. what is the outcome if you use the above? -- 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 tried this too :( -- 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 -~----------~----~----~----~------~----~------~--~---
Shai Rosenfeld wrote:>>> i believe the error appears because the LanguageHelper isn''t included >>> correctly and so the method isn''t actually embedded in the controller; >> >> I think it is included correctly, because in error message there is >> undefined method, but there is nothing like "unknown module >> LanguagesHelper". I think module is available, but method is not. (But >> of course I can be wrong). > > ..i''m not sure about this, but if you change > > module LanguagesHelper > def LanguagesHelper.sort_to_first(langs, lang) > ...my secret method here... > end > end > > to > > module LanguagesHelper > def sort_to_first(langs, lang) > ...my secret method here... > end > end >> and then in the controller, just put the method name (without prepending > it to LanguageHelper) like > > ordered_langs = sort_to_first(all_langs, lang) > > > i''ve had something similar, and i believe this worked for me. > what is the outcome if you use the above?Hi Pawel Stawicki, U put include instead of require it will work include LanguageHelper -- 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 -~----------~----~----~----~------~----~------~--~---