2010-12-13 64 views
9

我創建這樣的選擇框:如何控制Ruby on Rails 3中選擇框的大小?

f.select(:my_select_name, options_for_select(...)) 

我怎麼能控制選擇框的尺寸(寬)?

對於輸入文本字段我做的:

f.text_field(:field_name, :size => my_size) 

但對於「選擇」這是行不通的。

請指教。

回答

10

基本synatx的選擇是

select(object, method, choices, options = {}, html_options = {}) 

選項是通過下拉選項值更換和 可以更換html_options的寬度。

例如。 <% select("company", "branch_id", Branch.all.collect {|b| [ b.name, b.id ] }, { :prompt => "Select" }, {:class => "companySelect" })

對於select_tag可以使用

select_tag(name, option_tags = nil, options = {}) 

這裏的選項是類似的選擇html_options。

例如。 <%= select_tag 'company_name', options_for_select(get_company_name), :class => "select1"%>

有關詳細信息,請參閱select Tagselect

+0

嗨!我在哪裏可以看到'html_options'和'options'的所有可用選項? – 2010-12-13 09:06:06

+0

常用的html_options是:多個 - 允許多種選擇,:禁用 - 禁用輸入,:大小 - 設置大小,:class - 定義類,:onclick - 調用javascript onclick和其他鍵創建標準HTML屬性。 對於您可以選擇http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select – ssri 2010-12-14 05:26:20

+0

的選項謝謝,但我仍然不明白如何設置選擇的大小框。 ':size'對'select'不起作用(僅適用於文本字段)。你能否詳細說明一下? – 2010-12-14 13:46:11

14

在要點中,我使用:

<%= f.select(:my_select_name, [1,2,3,4,5], {}, :style => "width:70px") %> 

或者,如果我使用類似Twitter的引導,我使用一個類:

<%= f.select(:my_select_name, [1,2,3,4,5], {}, :class => "col-md-1") %> 
+0

謝謝,所以爲我工作。你也可以這樣做:<%= f.select(:foo_id,Foo.all.collect {| foo | [foo.name,foo.id,]},{},:style =>「width:250px」 )' – ksugiarto 2013-05-07 05:47:28

+0

謝謝它也適用於我。 – 2014-10-29 16:54:07