Forum: Ruby on Rails form.collection_select utilizing CONSTANT string array?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5849e5409f674d6aede51b4477d3dd7c?d=identicon&s=25 Coondog (Guest)
on 2009-04-24 23:16
(Received via mailing list)
Hello RoR World-

i've defined a number of string arrays in /lib/valid.rb which i'm
using to validate input for various fields, e.g.

module VALID
    TITLES = [ 'Mr.', 'Mrs.', 'Ms.', 'Dr.', 'Sir', 'RoRN00b', 'PhD.',
'DHH' ]
end

In my models I've done the following, which is working great for my
validations:
(from .../models/client.rb)
    include VALID
    validates_inclusion_of  :title,
      :in => TITLES,
      :message => '%s is Invalid - please enter a valid Title.'

Now I'd like to simplify my user's lives by utilizing these same
constants as the basis of my collection_selects in my assorted
partials, e.g.
(from .../views/clients/_form.erb)
<%= form.collection_select( :title, TITLES.all, TITLES[], TITLES[] )%>

My two questions:
1.  I'm receiving the error:
uninitialized constant ActionView::Base::CompiledTemplates::TITLES
despite having my obligatory 'include VALID' statement in my client.rb
model; tried adding to controller, no joy.
_help_.

2.  I'm sure my collection_select syntax above is broken; if/when i
get past the unintialized constant error i'm sure I can sort it
relatively quickly (i hope), but if anyone's done this before and
cares to share their code-fu I'd appreciate it.

Thanks much-
--Jeff Coon
5849e5409f674d6aede51b4477d3dd7c?d=identicon&s=25 Coondog (Guest)
on 2009-04-24 23:53
(Received via mailing list)
Believe that
<%= form.select( :title, options_for_select(TITLE), { :prompt =>
true } ) should work, if I can get the TITLES constant to be
recognized...

Thoughts?

Thanks-
--Jeff
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-04-26 17:24
(Received via mailing list)
The containing module for the ERB code isn't the model or the
controller, it's part of ActionView::Base.

Try explicitly qualifying the constant as Client::TITLES.

Also, module names are not traditionally spelled in all-caps, but
that's just a style thing.

--Matt Jones
5849e5409f674d6aede51b4477d3dd7c?d=identicon&s=25 Coondog (Guest)
on 2009-05-01 19:35
(Received via mailing list)
Hi Matt-

Thanks much -- qualifying fixed it.

        <%= f.select( :title, Contact::TITLES, { :prompt => true } )
%><br />

Really appreciate the quick tip-
--Jeff
This topic is locked and can not be replied to.