简体   繁体   中英

Create a Year Dropdown Box FormBuilder In Ruby On Rails

I am trying to create a db that has a year attribute but I am having some difficulty. I created the scaffold and tryied to modify the _form.html.erb with this code:

<%= f.date_select :year, :start_year=>2000, :end_year=>Time.now.year %>

Tried to run the rails server and gave me this error:

1 error(s) on assignment of multiparameter attributes

I realized that I only wanted the year and not the day or month. Is there a way to do that? I tried :discard_month=>true but that just hides it, but still storing it.

Thanks in advance

Do something like:

<%= f.date_select :year, :order => [:year], :start_year => 2000, :end_year => Time.now.year, :prompt => {:year => "Select year"} %>

Because your year field is an int and not a datetime :

<%= f.select :year, (2000..Time.now.year).to_a, :include_blank => {:year => "Select year"} %>

Another alternative is select_year

eg,

select_year(Date.current, start_year: 2015, end_year: Date.current.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