I can''t figure out how to get the collection_select to correctly
identify and select the appropriate option in the drop down to begin
with. i have checked that charge.offense_level contains the correct
value to begin with (a number between 1 and 10) but the view still
defaults to the first option and not the one contained in the
charge.offense_level. what am i doing wrong? any suggestions? I have
also tried: <%= collection_select "charge",
"offense_level",
@charge_types, :id, :full_name %> but that doesn''t work either.
@charges contains multiple records obtained from a find_by_sql query.
each record has 3 fields: charge_name, charge_id_number, offense_level
@charge_type.id should be equal to the @charge.offense_level
the view - update_charges.rhtml:
<h2>Add missing records to charges table</h2>
<%= start_form_tag %>
<table>
<th>Charge id number</th>
<th>Charge name</th>
<th>Classification level</th>
<%= render :partial => ''charge'', :collection =>
@charges %>
</table>
<%= submit_tag ''Save'' %>
<%= end_form_tag %>
the partial - _charge.rhtml:
<tr>
<td><%= hidden_field_tag
"charge[#{charge_counter}][charge_id_number]", charge.charge_id_number
%><%= charge.charge_id_number %></td>
<td><%= text_field_tag "charge[#{charge_counter}][name]",
charge.charge_name, :size => 50 %></td>
<td><select id = "charge[<%= charge_counter
%>][offense_level]"
name="charge[<%= charge_counter %>][offense_level]">
<%= options_from_collection_for_select @charge_types,
''id'',
''full_name'' %>
</select></td>
</tr>
here is the generated html from the source:
<tr>
<td><input id="charge[0][charge_id_number]"
name="charge[0][charge_id_number]" type="hidden"
value="52130005"
/>52130005</td>
<td><input id="charge[0][name]"
name="charge[0][name]" size="50"
type="text" value="Deadly Conduct Disch" /></td>
<td><select id = "charge[0][offense_level]"
name="charge[0][offense_level]">
<option value="1">Capital Felony</option>
<option value="2">First Degree Felony</option>
<option value="3">Second Degree Felony</option>
<option value="4">Third Degree Felony</option>
<option value="5">State Jail Felony</option>
<option value="6">Class A Misdemeanor</option>
<option value="7">Class B Misdemeanor</option>
<option value="8">Class C Misdemeanor</option>
<option value="9">Unknown Felony</option>
<option value="10">Unknown Misdemeanor</option>
</select></td>
</tr>
--
Posted via http://www.ruby-forum.com/.