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" }) %> But I got the same error. Curiously, if I use the following: <%= select("member", "sex", ["open", "womens", "coed"]) %> It doesn''t get that error, and generates the following output: <select id="member_sex" name="member[sex]"> <option>open</option> <option>womens</option> <option>coed</option> </select> I have nothing regarding "members" or "sex" in my DB or app. So what am I doing wrong? By the way, in trying to solve this problem, I checked the Rails API (looked at the source too), the HOWTOs, searched this list''s archives, Google, and asked on the IRC channel, so there''s no need to point me to any of those. If it''s helpful, here''s the relevant portion of the stack trace when I get the error: /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_view/ helpers/form_helper.rb:203:in `format'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_view/ helpers/form_helper.rb:203:in `send'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_view/ helpers/form_helper.rb:203:in `value'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_view/ helpers/form_options_helper.rb:200:in `to_select_tag'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_view/ helpers/form_options_helper.rb:20:in `select''
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 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
on Sun, 13 Feb 2005 22:23:32 Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> On 13.2.2005, at 21:12, Jon Bauman wrote: >> 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. > 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) %>Thanks for the help Jarkko. I did intend to put this list in the model eventually. I was just trying to reduce the number of variables in my problem by passing it directly. So, I took your advice and co-opted the method (which I just called kinds) to further validate the input: class Division < ActiveRecord::Base def self.kinds ["open", "womens", "coed"] end validates_inclusion_of :kind, :in => kinds, :message => "must be in #{kinds}" end The only problem is that in the error message #{kinds} gets expanded to "openwomenscoed" rather than "open, womens, coed". I figured out that I can make it do the latter by setting $, to ", ", but I''m not sure where exactly I should to that. I''d like it to apply for my entire application since I''ll be using this same pattern in several places. One other thing, when the validation methods return false, the problematic input elements are automatically rendered inside a div with a class of "elementsWithErrors" or something like that. That''s very handy, but is there any way to get the label for the input element included in that div?
Jon, On 15.2.2005, at 19:15, Jon Bauman wrote:> > Thanks for the help Jarkko. I did intend to put this list in the model > eventually. I was just trying to reduce the number of variables in my > problem by passing it directly. So, I took your advice and co-opted > the method (which I just called kinds) to further validate the input: > > class Division < ActiveRecord::Base > def self.kinds > ["open", "womens", "coed"] > end > validates_inclusion_of :kind, :in => kinds, :message => "must be in > #{kinds}" > endhow about "must be in #{kinds.join('','')}" ? Haven''t tested it, but it should be fine. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Tue, 15 Feb 2005 19:21:28 +0200, Jarkko Laine wrote:> On 15.2.2005, at 19:15, Jon Bauman wrote: >> >> Thanks for the help Jarkko. I did intend to put this list in the model >> eventually. I was just trying to reduce the number of variables in my >> problem by passing it directly. So, I took your advice and co-opted >> the method (which I just called kinds) to further validate the input: >> >> class Division < ActiveRecord::Base >> def self.kinds >> ["open", "womens", "coed"] >> end >> validates_inclusion_of :kind, :in => kinds, :message => "must be in >> #{kinds}" >> end > > how about "must be in #{kinds.join('','')}" ? > > Haven''t tested it, but it should be fine.I''m sure that would work fine, but I would like to change the default behavior for the entire app so that it can look pretty without having to add ".join('', '')" every time. Is that possible?
On 16/02/2005, at 7:38 AM, Jon Bauman wrote:> On Tue, 15 Feb 2005 19:21:28 +0200, Jarkko Laine wrote: >> On 15.2.2005, at 19:15, Jon Bauman wrote: >>> class Division < ActiveRecord::Base >>> def self.kinds >>> ["open", "womens", "coed"] >>> end >>> validates_inclusion_of :kind, :in => kinds, :message => "must be in >>> #{kinds}" >>> end >> >> how about "must be in #{kinds.join('','')}" ? >> >> Haven''t tested it, but it should be fine. > > I''m sure that would work fine, but I would like to change the default > behavior for the entire app so that it can look pretty without having > to add ".join('', '')" every time. Is that possible?How about adding a def self.kinds_formatted "#{kinds.join('', '')}#" end or something similar? - tim
Maybe Matching Threads
- FormOptionsHelper select call failing
- date_select where to add "class"=>"something"
- Parameters for FormOptionsHelper select vs. FormTagHelper select_tag
- Calling validates_inclusion_of out of default namespace
- Calling FormOptionsHelper select method in controller