在select_tag中提示

Kre*_*eki 16 ruby-on-rails ruby-on-rails-3

在我的用户注册申请中,我有一个国家选择器..

<%= select(:user, :country, options_for_select(@COUNTRIES)) %>
Run Code Online (Sandbox Code Playgroud)

我想添加一个提示作为第一个默认值(类似"--- select country ---").我应该在哪里以及如何选择此选项?

Jed*_*der 34

使用FormHelper :prompt

select(:user, :country, options_for_select(@COUNTRIES), {:prompt => "--select county--"})
Run Code Online (Sandbox Code Playgroud)

http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper


小智 7

collection_select(:product,
  :category_id,
  Category.all,    
  :id,    
  :title,    
  {:prompt => true}
)

collection_select(:product,    
  :category_id,    
  Category.all,    
  :id,    
  :title,    
  {:include_blank => 'Please Select'}
)
Run Code Online (Sandbox Code Playgroud)

这两个结果都会生成相同的 html,但当您返回编辑之前创建的产品时,第一个不会包含“请选择”选项