Rails select tag in form doesn't populate with value in the edit view

不打扰是莪最后的温柔 提交于 2020-01-04 13:48:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!