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 S.
require ālanguages_helperā
ordered_langs = LanguagesHelper.sort_to_first(all_langs, lang)
undefined method `sort_to_firstā for LanguagesHelper:Module
i 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ā)
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
Shai R. 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 S.,
U put include instead of require it will work
include LanguageHelper
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?