2010-07-16 124 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