Accessing Constants Declared in Helper Modules

Hello all,

I’m having great trouble accessing a constant declared in a helper for
one of my view templates. I am trying to create a grouped selection list
based on a predefined set of values. Rather than declare this set of
values in the view, I wanted to put it in the helper that automatically
gets included for the view. Unfortunately, it doesn’t seem to work.

All methods declared in the helper seem to work when called from the
view, but any referenced constants spit out a NameError and Rails tells
me I have an unitialized constant. I can get around this by referencing
the constant explicitly like this: MyHelperName::CONSTANT, but this
seems to defeat the elegance of using helpers in the first place.

My current solution is to wrap the constant in a method defined in the
helper file. Is there a way around this? I noticed this ticket
(http://72.14.209.104/search?q=cache:s1V2CcSpQDAJ:dev.rubyonrails.org/ticket/1790+rails+ticket+1790&hl=en&gl=us&ct=clnk&cd=1&client=safari)
describing what seems to be my problem, but it was posted a year ago and
I’m currently running Rails 1.1.6.

Is this a known bug? Am I doing something wrong? I was trying to follow
the example in AWDWR (2nd ed., Version: 2006-7-21, p.480), but I can’t
seem to get it to work.

Any thoughts would be greatly appreciated!

Best wishes,
SD

If app/helpers/application_helper.rb contains

 module ApplicationHelper
     MYCONST="Change"

then

 <%= ApplicationHelper.get_cost :MYCONST %>

will print “Change”.

It’s in the documentation for class “Module” in the pickaxe book.

Thanks for the reply, Robert. What I’m trying to do, however, is not
have to refer to the namespace where the constant is declared. I thought
the idea of a helper such as app/helpers/my_helper.rb is that its
methods and constants are included in app/controllers/my_controller.rb.

I was actually trying to use the example given in AWDWR (2nd ed.,
Version: 2006-7-21) on p.480, the code for which is here:

http://media.pragprog.com/titles/rails2/code/e1/views/app/helpers/test_helper.rb

http://media.pragprog.com/titles/rails2/code/e1/views/app/views/test/select.rhtml

The code in the view looks like this:

<%=
option_groups_from_collection_for_select(SHIPPING_OPTIONS,
:options, :type_name, # <- groups
:id,:name, # <- items
@order.shipping_option)
%>

Note how the SHIPPING_OPTIONS constant is referred to without prefixing
it with the helper module’s namespace.

I wanted to have something like that. Any thoughts?

Best wishes,
SD

Perhaps the difference in the test framework is that there’s a module
include or extend somewhere behind the scenes?

Dear Robert,

Thanks again for the reply. Unfortunately, to the best of my knowledge,
there’s nothing magical going on behind the scenes. So, I’m stumped.
Even when I try my code with an explicit “include”, I still can’t
reference the constant without getting an error.

I hope I am just being stupid and missing something simple, but I can’t
work out what it might be!

Hopefully someone else can pick up on this and provide some input.

Best wishes,
SD

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs