简体   繁体   中英

rails date_select, select year only

I have a form which allows user to select date, in the view i have it so the user only selects the year, but when the date is saved, it saves todays day and month along with it? all I want it to display is the year, which the user selects, this is my code:

<%= f.date_select :finishdate, :order => [:year] %><br>

你可以试试

<%= f.select_year :finishdate, :order => [:year] %><br>

如果希望仅在表格中显示年份值并将其存储在数据库中,而没有月份和日期,则可以有一个整数字段,并且仅显示年份值,如下所示:

<%= f.select :finishdate,Date.today.year-10 .. Date.today.year+10 %>

What is the end result that you'd like to work with? If it is only the year, you'll probably not want to use the date_select as that, I believe, will return a DateTime instance and not just the year.

If you do need just the year, you can look into other helpers like select_year .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM