Peter Jagielski
2006-Jan-01 17:00 UTC
[Rails] Can''t get select helper to populate dropdown w/default value
Guys, I searched this forum and found plenty of people having this problem, but I didn''t see any definitive solution. Usings Rails 1.0. When I edit an existing record, I can''t get the basic "select" helper to display the current value contained in the field of the record. All of my other (text) fields populate correctly with current field values. There appears to be a ":selected =>" option with the select helper and some of its variations, but I can''t get it to work. Frankly, I thought it would simply "work", in the same way that text fields get populated correctly. Am I doing something wrong, is the select helper broken, or is it simply not supposed to display the current value when editing? -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Jan-01 17:55 UTC
[Rails] Can''t get select helper to populate dropdown w/default value
Post some code for us to look at. There can be a lot of little things to look at in these cases, and its tough to help with out seeing the code. -Nick On 1/1/06, Peter Jagielski <pjagielski@gmail.com> wrote:> > Guys, > > I searched this forum and found plenty of people having this problem, > but I didn''t see any definitive solution. Usings Rails 1.0. > > When I edit an existing record, I can''t get the basic "select" helper to > display the current value contained in the field of the record. All of > my other (text) fields populate correctly with current field values. > > There appears to be a ":selected =>" option with the select helper and > some of its variations, but I can''t get it to work. Frankly, I thought > it would simply "work", in the same way that text fields get populated > correctly. > > Am I doing something wrong, is the select helper broken, or is it simply > not supposed to display the current value when editing? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060101/24b8a2a6/attachment.html
Kevin Olbrich
2006-Jan-01 18:10 UTC
[Rails] Re: Can''t get select helper to populate dropdown w/default v
Peter Jagielski wrote:> > Am I doing something wrong, is the select helper broken, or is it simply > not supposed to display the current value when editing?I suspect you are doing something wrong. If you use the select helpers, they will automatically select the correct value for you if you have them set up properly. One thing that could do it is if the values displayed in your dropdown do not match the values in your database. For instance, if you are displaying usernames, but storing user ids. If that is the case, make sure you populate the option array properly using something like this.. select("post", "person_id", Person.find_all.collect {|p| [ p.name, p.id ] }, { :include_blank => true }) The ''collect'' being the important part here. The first value is what gets displayed, and the second is the internal value used by the select dropdown. In this case, the @post.person_id value should be an ''id'', not a ''name''. -- Posted via http://www.ruby-forum.com/.
Peter Jagielski
2006-Jan-01 18:12 UTC
[Rails] Re: Can''t get select helper to populate dropdown w/default v
Nick, Here''s the hash that defines the contents of the dropdown: YESNO = { "No" => "0", "Yes" => "1" } In other words, the user sees options for "Yes" and "No, and either 0 or 1 are stored in the table''s field. In this case, the user can specify whether or not they are an administrator. Here''s my select helper code: <%= select ''user'', ''administrator'', User::YESNO.sort, :include_blank => TRUE %> I tried all manner of including :selected => ??? and it''s never worked. Now, I have found the following workaround, which solves the problem by using Rails'' options_for_select helper: <p><label for="user_administrator">Administrator</label><br/> <select id="user_administrator" name="user[administrator]"> <option value=""></option> <%= options_for_select User::YESNO.sort, @user.administrator.to_s %> </select></p> But going back to the original question, shouldn''t the basic select helper be able to handle this? -- Posted via http://www.ruby-forum.com/.
Peter Jagielski
2006-Jan-01 18:27 UTC
[Rails] Re: Can''t get select helper to populate dropdown w/default v
Kevin, I understand what you said, but I don''t know if I need to use the .collect the way you did, nor can I get it to work anyway. Since the data source for my dropdown is a hash and not the contents of a lookup table, and the dropdown itself is being populated correctly, I''m not sure if you your technique fits my situation. Suggestions? -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Jan-01 18:30 UTC
[Rails] Re: Can''t get select helper to populate dropdown w/default v
Well thats the way I do it Peter, using the options helper that is. By looking at the docs for the select helper, it doesn''t need/have a selected value, it should automatically pick the right one. Like I said, I usually always use the options helper and never really had any problems with that. -Nick On 1/1/06, Peter Jagielski <pjagielski@gmail.com> wrote:> > Nick, > > Here''s the hash that defines the contents of the dropdown: > > YESNO = { "No" => "0", > "Yes" => "1" } > > In other words, the user sees options for "Yes" and "No, and either 0 or > 1 are stored in the table''s field. In this case, the user can specify > whether or not they are an administrator. > > Here''s my select helper code: > > <%= select ''user'', ''administrator'', User::YESNO.sort, :include_blank => > TRUE %> > > I tried all manner of including :selected => ??? and it''s never worked. > > Now, I have found the following workaround, which solves the problem by > using Rails'' options_for_select helper: > > <p><label for="user_administrator">Administrator</label><br/> > <select id="user_administrator" name="user[administrator]"> > <option value=""></option> > <%= options_for_select User::YESNO.sort, @user.administrator.to_s %> > </select></p> > > But going back to the original question, shouldn''t the basic select > helper be able to handle this? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060101/9e836bcb/attachment.html
Peter Jagielski
2006-Jan-01 18:33 UTC
[Rails] Re: Can''t get select helper to populate dropdown w/default v
Guys, Ok, problem solved. I changed my hash from: YESNO = { "No" => "0", "Yes" => "1" } to: YESNO = { "No" => 0, "Yes" => 1 } The field''s type in my table is SMALLINT, so now the hash matches. So...the following code works: <%= select ''user'', ''administrator'', Bug::YESNO, :include_blank => TRUE %> Thanks Kevin & Nick! Happy New Year! Hope this helps the other folks getting this kind of thing to work! -- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jan-02 06:09 UTC
[Rails] Re: Re: Can''t get select helper to populate dropdown w/defau
Nick Stuart wrote:> Well thats the way I do it Peter, using the options helper that is. By > looking at the docs for the select helper, it doesn''t need/have a > selected > value, it should automatically pick the right one. Like I said, I > usually > always use the options helper and never really had any problems with > that. > > -Nickincidentally, if you try to use the :prompt option for the select helpers, make sure that your database table can accept a null for that value. The prompt is only selected by default when the corresponding model value is a null. -- Posted via http://www.ruby-forum.com/.