How to use select_year with form_for?

I have the following form:

<%= form_for(@car, :html => {:class => 'form-horizontal' }) do |f| %> <% if @car.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@car.errors.count, "error") %> prohibited this car from being saved</h2> <ul> <% @car.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :price %><br /> <%= f.text_field :price %> </div> <div class="field"> <%= f.label :model_year %><br /> <% %> <% end %> </div> 

I want the selection of a collection for the model year to start 100 years ago and be about a year in the future. I tried

 <%= f.select_year :model_year%> 

but he says select_year is not a valid method. I tried

 <%= f.date :model_year, :discard_month => true %> 

also to no avail. Any thoughts?

+7
date ruby-on-rails simple-form form-for
source share
1 answer

select_year is a helper that generates a full select tag with parameters. Since you are using form_for instead of form_tag , you need to use a helper element that can be called in the form builder object.

 <%= f.select :model_year, (Time.zone.now.year - 100)..(Time.zone.now.year + 1) %> 

Link: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_year

+9
source share

All Articles