Where to put a large constant?

In a registration form I allow the user to select several spoken
languages and of course to minimize tomfoolery I am using selects. In
the view I have:

<%= select(‘person’, ‘language1’, LANGUAGES, options = {}) %>

In my people_helper.rb I have:

module PeopleHelper

constant for language selection in ‘_form_background.rhtml’

LANGUAGES = %w{Akan
Algerian
Amharic
Arabic

Xhosa
Yoruba
Zulu }

end

However rails still complains, “uninitialized constant LANGUAGES.” I
can put it above my person.rb model but that is messy. Is there a
better way?

from what i know, and from what works for me, i put it in enviornment.rb
file.
as i understand, all of ruby’s constants are loaded when the server is
first started, so enviornment.rb is the given place to put it.
whether the theory, or logical structure is correct, i don’t know - -
but it works great. (after restarting the server, obviously).

hth,

harp

Worked perfectly. Thanks!

That is a great solution. It definately makes sense to only initialize
the array when it is needed and not globally. Every time you learn
something, you realize there is always a better way right after you
finish implementing the solution!!! Thanks to everyone who replied!!!

That solution is still a little on the messy side (in terms of
cluttering up environment.rb). In addition, you have the (admittedly
very minor) overhead of initializing the array before any
controller/action called, even though only the controller dealing with
the Person model actually uses it.

What I would do would be to create a languages.rb file in your lib
directory with nothing in it except the constant definition. Then call
“require ‘languages’” at the top of the appropriate controller. In fact,
if it is only used in one action, you can even call it just for that
specific action:

class PeopleController < ApplicationController

def register
require ‘languages’
# whatever else your action needs
@person = Person.new

...

end

end

Just a different (and cleaner, in my opinion) way to do it. Hope this
helps!

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