在窗体中的Rails选择标签不会在编辑视图中填充值 [英] Rails select tag in form doesn't populate with value in the edit view

查看:182
本文介绍了在窗体中的Rails选择标签不会在编辑视图中填充值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用脚手架为成员模型生成了典型的rails _form。对于param:状态,我使用了一个select标签:

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

而对于parammember_since,我使用了select_year帮助器:

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?

推荐答案

<%= 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"} )   %>

UPDATE:

由于member_since是字符串,您需要将它转换为日期:

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"} )   %>

这篇关于在窗体中的Rails选择标签不会在编辑视图中填充值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆