How to create and use RoR module?

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 :frowning: 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 :frowning:

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

I tried this too :frowning:

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?