[Rails] Select Enumerated Values with FormOptionsHelper
Jarkko Laine
jarkko at jlaine.net
Sun Feb 13 20:23:32 GMT 2005
Hi Jon,
On 13.2.2005, at 21:12, Jon Bauman wrote:
> Hey,
>
> I'm new, so apologies if I've overlooked obvious resources or violated
> any etiquette rules for this list.
>
> Since rails does not support the MySQL enum data type, I'm following
> David's advice of using a varchar and storing the valid values in the
> application:
> http://one.textdrive.com/pipermail/rails/2005-January/001536.html
>
> So here's my situation. I have a table called "divisions" with a
> column named "format". It's a varchar, but only the following values
> are valid: ["open", "womens", "coed"]. Naturally, I want to have an
> HTML select element on the edit page that contains the valid values,
> and is set to the current value. It seems like
> FormOptionsHelper#select should do exactly what I want, so I tried
> this:
>
> <%= select("division", "format", ["open", "womens", "coed"]) %>
>
> But that gets a "raised too few arguments" error. Based on this HOWTO:
> http://wiki.rubyonrails.com/rails/show/HowtoUseFormOptionHelpers
> I tried changing it to this:
>
> <%= select("division", "format", { "open" => "open", "womens" =>
> "womens", "coed" => "coed" }) %>
Format is a Ruby kernel method so it's a reserved word you shouldn't be
using in your app. The error probably means that format gets executed
with no arguments.
BTW, if you have a list of valid choices (like enum), you should
probably build that list to your model class so you wouldn't need to
retype it in every form.
class KlassName < Application...
def self.ValidFormats
["open", "womens", "coed"]
end
end
Then you could use this same list in your validations
(http://rails.rubyonrails.com/classes/ActiveRecord/Validations.html)
and your forms: <%= select("objname", "methodname",
KlassName.ValidFormats) %>
--
Jarkko Laine
http://jlaine.net
http://odesign.fi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2363 bytes
Desc: not available
Url : http://one.textdrive.com/pipermail/rails/attachments/20050213/f442ec50/smime.bin
More information about the Rails
mailing list