Issue with “:prompt” in select_year with start/end_year

假装没事ソ 提交于 2019-12-24 16:11:17

问题


Using this answer as a guide, I set out to create a select_year that started with today's year and ends 100 years ago. But I wanted to add a :prompt so that the drop down starts with "Year" instead of the current year. So I used the following:

<%= select_year(Date.today, {:prompt => "Year", :start_year => DateTime.now.year, :end_year => DateTime.now.year - 115}, {:field_name => 'Year', :id => 'Date.year'}) %>

This renders a drop down for year, but the prompt shows current year instead of "Year". If I click the drop down though, it shows "Year" as the first option with current year selected. How can I fix this? What did I do wrong?

UPDATE: Here is the HTML output of the above code:

<select field_name="Year" id="Date.year" name="date[year]">
<option value="">Year</option>
<option selected="selected" value="2011">2011</option>

How can I make it so 2011 isn't automatically "selected"?


回答1:


solution was simple

    <%= select_year(0, {:prompt => "Year", 
                        :start_year => DateTime.now.year, 
                        :end_year => DateTime.now.year - 115}, 
                       {:field_name => 'Year', :id => 'Date.year'}) %>

I suppopse if default value is out of range you have first of the list selected which is prompt



来源:https://stackoverflow.com/questions/7060216/issue-with-prompt-in-select-year-with-start-end-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!