问题
I have the typical rails _form generated by scaffold for a member model. For the param :state I am using a select tag:
<%= f.select :state, options_for_select(us_states), { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
Using a helper method (us states):
def us_states
[
['AK', 'AK'],
['AL', 'AL'], #etc
And for the param "member_since" I am using the select_year helper:
<%= select_year(0, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
Now both of these selects work to create a new record, but neither field is pre-filled in the edit record view. Any thoughts?
回答1:
<%= f.select :state, us_states, { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
and
<%= select_year(f.object.member_since, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
UPDATE:
Since member_since is a string, you will need to convert it to date:
<%= select_year(Date.new(f.object.member_since.to_i), {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
来源:https://stackoverflow.com/questions/20497503/rails-select-tag-in-form-doesnt-populate-with-value-in-the-edit-view