使用数据库值重新填充下拉菜单(rails)

时间:2013-08-11 04:55:46

标签: ruby-on-rails ruby drop-down-menu edit populate

当转到表单的“编辑”视图时,如果先前已将值发布到数据库,则会重新填充所有字段,但下拉菜单字段除外。例如,“惊讶”值现在位于数据库中。当我打开编辑页面时,我只看到默认的“选择选项...”

以下是我的表格:

<%= f.select :emotion, options_for_select([["Happy", "Happy"], ["Afraid", "Afraid"], ["Sad", "Sad"], ["Surprised", "Surprised"], ["Inspired", "Inspired"]]), :include_blank => 'Select Option...' %>

如何让它重新填充?

2 个答案:

答案 0 :(得分:0)

使用:

options_for_select([["Happy", "Happy"], ["Afraid", "Afraid"], ["Sad", "Sad"], ["Surprised", "Surprised"], ["Inspired", "Inspired"]], <selected_value>)

答案 1 :(得分:0)

想出来了!将:include_blank的值更改为数据库中的实际变量。

<%= f.select :emotion, ([["Happy", "Happy"], ["Afraid", "Afraid"], ["Sad", "Sad"], ["Surprised", "Surprised"], ["Inspired", "Inspired"]]), :include_blank => @table_name.emotion %>