I have just built a migration for my movies table called year_id When I create two new years, 2012 and 2013, I then add the dropdown to select the year and I get this:
How can i make my dropdown select show the actual year (2012 or 2013) and not #< Year:0x000 etc...
This is my model:
class Year < ActiveRecord::Base
attr_accessible :year
has_many :movies
end
This is my form:
<%= semantic_form_for @movie, :html => { :multipart => true } do |f| %>
<% if @movie.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@movie.errors.count, "error") %> prohibited this movie from being saved:
</h2>
<ul>
<% @movie.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field"> <%=h f.input :year, :include_blank => false %> </div><br />
Without seeing the full code for the form it is difficult to answer your question exactly. However, what is happening is the actual instance of your Year
is being passed as the option text. You would probably see a similar output if you called to_s
from the console
Year.first.to_s
# => "#<Year:0x00000101bcea10>"
Take a look at the options_for_select
documentation at http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select to see how to properly define a select element's options.
It looks like you might also be able to use the collection_select
form helper to save yourself the trouble of defining the options array. It would look something like this
<%= f.collection_select :year_id, Year.all, :id, :year %>
The last option :year
is the method that is used for the option text, so you'd change that to something meaningful for your model.