2012-04-19 40 views

回答

3

大概你會以英寸存儲它,所以你可以生成你的@heights傳遞給options_from_collection_for_select像:

(56..112).to_a.map { |inch| { id: inch.to_s, name: (inch/12).floor.to_s+'\''+(inch%12).to_s } } 

與英寸到字符串轉換分解成一個輔助方法也許。

+0

謝謝,它的工作。 – johnnewbie 2012-04-30 00:25:19

+1

只是一個微小的修正,你要通過12不是14 – 2012-08-21 06:46:22

+0

@PaulPettengill呃,好點來劃分。我想我一定是一直在思考磅石。編輯。 – Colin 2012-08-23 10:29:50

2

不知道這是最好的方式, 但你可以嘗試這樣的

@heights = (4..7).to_a.collect{|o| 0.upto(11).to_a.collect{|k| ["#{o.to_s+'\'' + k.to_s}",o.to_s + k.to_s]}}.inject([]){|s,v| s | v} 

或更好一些事情,

@heights = (4..7).to_a.collect{|o| 0.upto(11).to_a.collect{|k| ["#{o.to_s+'\'' + k.to_s}",o.to_s + k.to_s]}}.flatten(1) 

本應給予的結構類似,

[["4'0", "40"], ["4'1", "41"], ["4'2", "42"], ["4'3", "43"], ["4'4", "44"]] # and so on.. 

現在,要爲選擇選項準備此結構,您可以使用:

@heights.map { |name, id| OpenStruct.new(:value => id, :name => name) } 

這將給名稱和值對每個高度元素,可用於製作選項中選擇標籤

您可能需要包括OpenStruct,所以使用

require 'ostruct' 

把這在選擇標籤:

<%= select_tag "Height", options_from_collection_for_select(@heights,'value','name'), html_options = { :onChange=> "height_changed();"} %> 
0

這是人類高度 https://github.com/AlexanderZaytsev/height

寫一個幫助一個好的寶石

def height 
(121..244).to_a.collect{|k| ["#{Height.new(k).to_s(:default, :metric)}","#{Height.new(k).to_s(:default, :imperial)}","#{Height.new(k).feet}"]} 
end 

輸出

[["1m 21cm", "4 ft 0 in", "4"], ["1m 22cm", "4 ft 0 in", "4"], ["1m 23cm", "4 ft 0 in", "4"], ["1m 24cm", "4 ft 1 in", "4.08"] 

使用模板的方法

<%= f.input :how_tall_are_you, label: "How tall are you ? (in feet and Cm)", collection: height.map { |cm, inc, value| ["#{cm} - #{inc}", "#{value}"] } ,prompt: "-Please select one-",input_html: { class: 'form-control input-lg', required: true} %> 
0

科林的回答讓我的存在方式的一部分:

<%= 
    n.select :height, 
    options_for_select((36..96).to_a.map { |inch| 
    { 
     id: inch.to_s, 
     name: (inch/12).floor.to_s+'\''+(inch%12).to_s+'"' 
    } 
    }), 
    {}, 
    id: 'height' 
%> 

在視圖中,在下拉菜單中顯示的是這樣的:

height dropdown

呈現這樣的HTML:

<select id="height" name="height_select"> 
<option value="{:id=>&quot;36&quot;, :name=>&quot;3'0\&quot;&quot;}">{:id=&gt;"36", :name=&gt;"3'0\""}</option> 
<option value="{:id=>&quot;37&quot;, :name=>&quot;3'1\&quot;&quot;}">{:id=&gt;"37", :name=&gt;"3'1\""}</option> 
<option value="{:id=>&quot;38&quot;, :name=>&quot;3'2\&quot;&quot;}">{:id=&gt;"38", :name=&gt;"3'2\""}</option> 
.... 

爲了讓它僅顯示高度,我添加了一個收集並指定要顯示的內容(名稱)。我還指定了一個默認值(user_height)。

<%= 
    n.select :height, 
    options_for_select(
    (36..96).to_a.map { |inch| 
     { 
     id: inch.to_s, 
     name: (inch/12).floor.to_s+'\''+(inch%12).to_s+'"' 
     } 
    }.collect{ |p| 
     [p[:name], 
     p[:id].to_i] 
    }, 
    user_height), 
    {}, 
    id: 'height' 
%> 

height dropdown leo

HTML渲染:

<select id="height" name="height_select"> 
<option value="36">3'0"</option> 
<option value="37">3'1"</option> 
<option value="38">3'2"</option> 
... 
<option selected="selected" value="69">5'9"</option> 
... 
</select> 

我不知道這是否是最有效的解決方案,但它的工程!

這裏是some relevant documentation