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

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Richard B. (Guest)
on 2007-06-01 06: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

Thanks in Advance

Steve R. (Guest)
on 2007-06-01 16: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 R. (Guest)
on 2007-06-01 21: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.

Richard B. (Guest)
on 2007-06-01 23: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.

This topic is locked and can not be replied to.