在Rails中使用select_month时获取空白选项

时间:2011-07-07 20:42:41

标签: html ruby-on-rails

我无法在Rails中获取date_select标记以显示空白选项。这是我的代码:

<%= select_month(@foo, :include_blank => true) %>

但是该代码在没有空白选项的情况下呈现此HTML。

<select id="month" name="month"><option value="1">January</option>
    <option value="2">February</option>

    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>

    <option value="12">December</option>
</select>

我忽略了什么吗?

1 个答案:

答案 0 :(得分:1)

您使用的是哪个版本的导轨?我刚刚在控制台(Rails 3.0.8)中尝试过它,它可以工作:

ruby-1.9.2-p0 > include ActionView::Helpers::DateHelper
ruby-1.9.2-p0 > select_month( Time.now )

=> <select id="date_month" name="date[month]">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option selected="selected" value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

ruby-1.9.2-p0 > select_month( Time.now, :include_blank => true )
=> <select id="date_month" name="date[month]">
<option value=""></option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option selected="selected" value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>