I am writing a personal genealogy app with Ruby on Rails 5, and am using the :date datatype for entering birth/death dates. This results in a dropdown menu for each element (year/month/day) of a date. For the year, the menu defaults to a range of five years in the past to five years in the future (so for 2017, the range is from 2012 to 2022). But I need to have a much larger range to cover years going back multiple centuries. How can I define a specific range of years I want to be available in the dropdown menu?
我正在使用Ruby on Rails 5编写一个个人族谱应用程序,并使用:date数据类型输入出生/死亡日期。这导致日期的每个元素(年/月/日)的下拉菜单。对于这一年,菜单默认为过去五年至未来五年(因此2017年的范围是2012年至2022年)。但我需要有更大的范围来覆盖多个世纪以来的岁月。如何在下拉菜单中定义我想要的特定年份范围?
Here's an excerpt from my schema.rb file (other fields deleted for brevity):
这是我的schema.rb文件的摘录(为简洁起见,删除了其他字段):
create_table "people", force: :cascade do |t|
t.date "birth_date"
t.date "death_date"
end
And here's and excerpt from my view file (again, other fields deleted for brevity):
这里和我的视图文件摘录(再次,为简洁起见删除了其他字段):
<div class="booyah-box col-10 offset-1">
<h1>Edit Your Person Details</h1>
<%= simple_form_for @person do |f| %>
<%= f.input :birth_date %>
<%= f.input :death_date %>
<%= f.submit 'Update', class: 'btn btn-primary' %>
<% end %>
</div>
I should also add I need a way to have a "no choice"-type option, such as for those still living, since they wouldn't have a death date
我还应该补充一点,我需要一种方法来获得“无选择”类型的选项,例如对于那些仍然生活的人,因为他们没有死亡日期
I've searched on * and haven't found an applicable answer, but if there's already one here, please point me to it. Thanks in advance for your help.
我在*上搜索过但没有找到合适的答案,但是如果已经有一个答案,请指点我。在此先感谢您的帮助。
1 个解决方案
#1
1
You can specify the start_year
and end_year
in the date input
within your view; for example:
您可以在视图中的日期输入中指定start_year和end_year;例如:
<div class="booyah-box col-10 offset-1">
<h1>Edit Your Person Details</h1>
<%= simple_form_for @person do |f| %>
<%= f.input :birth_date, start_year: Time.now.year - 10, end_year: Time.now.year + 10 %>
<%= f.input :death_date, start_year: Time.now.year - 10, end_year: Time.now.year + 10, prompt: "-" %>
<%= f.submit 'Update', class: 'btn btn-primary' %>
<% end %>
</div>
In the example I added 10 years from current date and 10 years after current date, but you can use any range you need.
在示例中,我从当前日期和当前日期后10年添加了10年,但您可以使用您需要的任何范围。
#1
1
You can specify the start_year
and end_year
in the date input
within your view; for example:
您可以在视图中的日期输入中指定start_year和end_year;例如:
<div class="booyah-box col-10 offset-1">
<h1>Edit Your Person Details</h1>
<%= simple_form_for @person do |f| %>
<%= f.input :birth_date, start_year: Time.now.year - 10, end_year: Time.now.year + 10 %>
<%= f.input :death_date, start_year: Time.now.year - 10, end_year: Time.now.year + 10, prompt: "-" %>
<%= f.submit 'Update', class: 'btn btn-primary' %>
<% end %>
</div>
In the example I added 10 years from current date and 10 years after current date, but you can use any range you need.
在示例中,我从当前日期和当前日期后10年添加了10年,但您可以使用您需要的任何范围。