简体   繁体   中英

date_select in rails , How do I get the selected value?

My view file is below.

<div class="field">
    <%= f.label :deadline , class:"form-label"  %><br />
    <%= f.date_select( :deadline , use_month_numbers: true , start_year: (Time.now.year) , end_year: (Time.now.year+1) , start_month: (Time.now.month) , end_month: (12) , start_day: (Time.now.day) , end_day: (days_in_month(Time.now.month)) , default: Date.new(Time.now.year , Time.now.month , Time.now.day) , date_separator: '/' ) %>
</div>

days_in_month(Time.now.month) is helper method.

def days_in_month(month, year = Time.now.year)
   common_year_days_in_month = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
   return 29 if month == 2 && Date.gregorian_leap?(year)
   common_year_days_in_month[month]
end

In this case , when user selects 'month', 'day' doesn't change. For example, in 2016-12-28, The page shows 2016/12/28 in default, and the end_day is 31 because 12 month has 31 days. But although user selects 11 month , end_day is 31 because 12 month has 31 days. in the case , I want to show 30 in end_day. Please help me...

That's a client side matter, you should handle it using javascript. This may help you: Form days value change with respect to the month .

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