簡體   English   中英

在select_tag中提示

[英]Prompt in select_tag

在我的用戶注冊申請中,我有一個國家選擇器..

<%= select(:user, :country, options_for_select(@COUNTRIES)) %>

我想添加一個提示作為第一個默認值(類似“--- select country ---”)。 我應該在哪里以及如何選擇此選項?

使用FormHelper :prompt

select(:user, :country, options_for_select(@COUNTRIES), {:prompt => "--select county--"})

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

您也可以像這樣給出自定義的提示值

select(:user, :country, options_for_select(@COUNTRIES), :prompt=>"select User name")

非常簡單:

select(:user, :country, options_for_select(@COUNTRIES), :prompt=>true)

對於提示“請選擇”,或者為您的自定義文本:

select(:user, :country, options_for_select(@COUNTRIES), :prompt=>"Select country")

還要注意@COUNTRIES是錯誤的,實例變量應該是小寫的 - @countries ,一個@countries就是COUNTRIES

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

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

這兩個都導致相同的html,但當您返回編輯以前創建的產品時,第一個將不包括“請選擇”選項

如果有人提到這個,請嘗試在options_for_select之外寫prompt

代替

select(:user, :country, options_for_select(@COUNTRIES), :prompt=>true)

嘗試,

select(:user, :country, options_for_select(@COUNTRIES)),{:prompt=>"Your message here"}

也適用於select_tag。 此外,我同意@gunn的命名約定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM