我有一个由成员模型的脚手架生成的典型rails _form。对于参数:状态我正在使用选择标记:
<%= f.select :state, options_for_select(us_states), { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
使用辅助方法(我们陈述):
def us_states
[
['AK', 'AK'],
['AL', 'AL'], #etc
对于参数“member_since”,我正在使用select_year帮助器:
<%= select_year(0, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
现在,这两个选项都可以创建新记录,但在编辑记录视图中都没有预填充任何字段。有什么想法吗?
答案 0 :(得分:3)
<%= f.select :state, us_states, { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
和
<%= select_year(f.object.member_since, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
更新:
由于member_since是一个字符串,您需要将其转换为日期:
<%= 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"} ) %>