Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails associations and forms

I have a model called appointment. Each appointment has a stylist. In the form where I create a new appointment, I'm doing this:

  <div class="field">
    <%= f.label :stylist_id %><br />
    <%= f.select(:stylist_id, Stylist.order("name").map { |s| [s.name, s.id] }) %>
  </div>

This works but it's going to be tedious to do this kind of thing for every association in my app. I imagine Rails has some way of automatically generating select fields for associations but I have no idea how it works. Does such a thing exist?

Oh, and by the way, I already know about scaffolding. If scaffolding is supposed to take care of what I describe above, I'm apparently doing something wrong, because it's not doing that for me.

(I'm on Rails 3.)

like image 840
Jason Swett Avatar asked Jan 08 '11 21:01

Jason Swett


1 Answers

Hmm, it seems that collection_select would work for the typical scenario:

<%= f.collection_select :stylist_id, Stylist.all, :id, :name %>
like image 162
prusswan Avatar answered Oct 18 '22 06:10

prusswan