Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use select_year with form_for?

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?

like image 289
mano26 Avatar asked Oct 08 '13 23:10

mano26


1 Answers

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

like image 56
Marcelo De Polli Avatar answered Sep 23 '22 20:09

Marcelo De Polli