Forum: Ruby on Rails Edit method: Populating select box values

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.
Daaf7741b86c0eb622c514e35ca1c608?d=identicon&s=25 Richard Back (reback)
on 2007-06-01 04:02
I have an employees table that contains fields that are boolean to
control information formatting.  The 'new' method sets the value of the
select box to the default value contained in the table.  During the
'edit' method these values are the table defaults not the values
contained in the table.  How can I change the following excerpt of
_form.rhtml to use the table values contained in the record?  Note:
This is the code generated by the scaffold controller.

<p><label for="employee_pubadd">Publish Address</label>
<select id="employee_pubadd" name="employee[pubadd]">
<option value="false">False</option><option
value="true">True</option></select></p>

Thanks in Advance

reback
19990e3fa0fc68c874f42f13d1500011?d=identicon&s=25 Steve Rawlinson (trip)
on 2007-06-01 14:39
As it stands, that is just static html. It's not picking a default from
anywhere. You need to use the select method as embedded ruby and add the
:selected option. It will look something like this:

<%= select 'employee', 'pubadd', [["true", "true"] , ["false",
"false"]], :selected => @employee.pubadd ? @employee.pubadd : "false" %>


I havent actually tried that but a bit of fiddling should get it doing
what you want.

steve
19990e3fa0fc68c874f42f13d1500011?d=identicon&s=25 Steve Rawlinson (trip)
on 2007-06-01 19:02
> <%= select 'employee', 'pubadd', [["true", "true"] , ["false",
> "false"]], :selected => @employee.pubadd ? @employee.pubadd : "false" %>

Actually it looks like you don't need the :selected option, rails does
it anyway if @employee is set.

steve
Daaf7741b86c0eb622c514e35ca1c608?d=identicon&s=25 Richard Back (reback)
on 2007-06-01 21:07
Thanks Steve,

That works great!

I knew that the html generated from the scaffolding was static.  However
I am surprised that the rails generators are not better at handling
boolean type data fields.

Richard
This topic is locked and can not be replied to.