Ruby on Rails中select的未定义值

时间:2012-08-24 12:43:22

标签: ruby-on-rails ruby-on-rails-3.2

我只想为select设置未定义的值。未定义我的意思是“未定义”,但不仅仅是默认值。

我有这个:

= form_for @address do |f|
  = f.select :country_id, [['no country selected','0']] + Country.as_key_value_array

其中Country.as_key_value_array返回[['country1','1'], ['country2','3'],['country3','3']]

数组

有没有其他方法可以达到我的目的?

2 个答案:

答案 0 :(得分:2)

f.select :country_id, "<option value='0'>no country selected</option>".html_safe + options_from_collection_for_select(Country.all, :id, :name)

答案 1 :(得分:0)

检查一下,

= f.select :country_id, options_for_select([['no country selected','0']] + Country.as_key_value_array)