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?