poombavai
poombavai

Reputation: 111

Using f.select for year drop down default to current year in rails

I am using drop down for selecting year in my form. How do I make my drop down defaults to current year. By using select_year method, we can use like

<%= select_year (Date.today, start_year: Date.today.year-1, end_year: Date.today.year+1) %>

Similarly how do I use it my form_for?

<%= i.select :year, (Date.today.year-1)..(Date.today.year+1), :selected => Date.today.year %>

Thanks in Advance.

Upvotes: 1

Views: 981

Answers (2)

hoangdd
hoangdd

Reputation: 509

I think it should be:

<% year = Date.today.year %>
<%= i.select :year, options_for_selection((year-1)..(year+1), year)  %>

Hope this helps.

Upvotes: 0

Vishal
Vishal

Reputation: 7361

You can use this

<%= i.select :year, (Date.today.year-1)..(Date.today.year+1), :selected => Time.current.year %>

Upvotes: 2

Related Questions