Topic: Drop Down Menu Problem

Here is my papers table:

title:string
duration:integer
exam_at:datetime
paper_id:integer
author:string

and here is my author table:

name:string
email:string
telephone:string
author_id:string

I have created views for both of these table, and I have implemented the CRUD structure for them both, but there is one small problem I have. when it comes to creating a new paper, and generating the value for the author value in the papers table, I want to use a select drop down menu that consists of the names of all the authors in the authors table (so i can select which author created the paper).

right now I have the html code displayed like this:

<% form_for(@paper) do |f| %>
  <%= f.error_messages %>

  <%= f.label :title %><br />
  <%= f.text_field :title %><br />

  <%= f.label :duration %> (Minutes)<br />
  <%= f.text_field :duration %><br />
 
  <%= f.label :exam_at %><br />
  <%= f.datetime_select :exam_at %>
 
  <%= f.label :paper_id %> ID<br />
  <%= f.text_field :paper_id %>
 
  <%= f.label :author %><br />

 
  #AREA OF CODE TO BE FOCUSED ON

  <select name="select", :author>
    <option>Select Author...</option>
    <% for author in @authors %>
      <option><%=h author.name %></option>
    <% end %>
  </select>

  #AREA OF CODE TO BE FOCUSED ON


  <%= f.submit "Create" %>
  <% end %>

Obviously this code will produce a drop down menu that shows the names of all the authors, but will not save to the table, because it is not being held by 'f'. I was just wondering if anybody new the code for this (should start with "<%= f.select" I believe.

Hope someone can help smile

Re: Drop Down Menu Problem

There are several alternatives for the select statement - details in the actionview api.

In this it should be possible to work with the collection_select:

<% fields_for :author do %>
<%= f.collection_select(:name, Author.find(:all), :author_id, :name %>

<% end %>


This should provide the author_id in the params-hash.

cheers, rainer

Re: Drop Down Menu Problem

thank you,

the for loop didn't work properly because it started to display a number of collection selects that were all the same, but when i took away the for loop (leaving just the collection select) i got one drop down menu showing all authors. thanks for that.