I have a form as follows:
<%= 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 to have a collection select for the model year starting 100 years back and going to a year in the future. I have tried
<%= f.select_year :model_year%>
but it says that select_year is not a valid method. Tried
<%= f.date :model_year, :discard_month => true %>
also to no avail. Any thoughts?
select_year
is a helper that generates a full select tag with options. Since you're using form_for
instead of form_tag
, you'll want to use a helper that can be called on a form builder object.
<%= f.select :model_year, (Time.zone.now.year - 100)..(Time.zone.now.year + 1) %>
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_year
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With