2010-07-16 103 views
6

我有一個Timesheet模型,一個Run模型和一個Athlete模型。Collection_select在嵌套模型中

class Timesheet < ActiveRecord::Base 
    has_many :runs, :dependent => :destroy 
    accepts_nested_attributes_for :runs 
end 


class Run < ActiveRecord::Base 
    belongs_to :timesheet 
    belongs_to :athlete 
end 

class Athlete < ActiveRecord::Base 
    has_many :runs 
end 

運行被嵌套在時間表,我想創建我創建了一個時間表相同的形式對一些運行,如本railscast

class TimesheetsController < ApplicationController 
    def new 
    @timesheet = Timesheet.new 
    3.times { @timesheet.runs.build } 
    end 

在我的時間表的形式,我遇到我的collection_select(在運行表中填入athlet_id字段中的運動員名稱的下拉列表)的問題。

​​

是否有可能來填充:使用collection_select一個運行的athlete_id場如上圖所示,嵌套形式時間表內,還是我失去了一些東西?

回答

15

它看起來像你沒有創造集合的形式製作工具中選擇,嘗試這樣的事情:

<% f.fields_for :runs do |r| %> 
    <%= r.collection_select(:athlete_id, Athlete.all(:order => 'name'), :id, :name, { :prompt => 'Select Athlete' }) %> 
<% end %> 
+1

謝謝!這工作完美,讓我非常沮喪。 – jktress 2010-07-17 04:52:50