我有时间表模型,运行模型和运动员模型.
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
Run Code Online (Sandbox Code Playgroud)
运行嵌套在Timesheets下,我想在创建Timesheet的同一表单上创建一些运行,如此railscast中所示
class TimesheetsController < ApplicationController
def new
@timesheet = Timesheet.new
3.times { @timesheet.runs.build }
end
Run Code Online (Sandbox Code Playgroud)
在我的时间表表单上,我遇到了我的collection_select问题(运动员名称的下拉列表填充了运行表中的:athlete_id字段).
<% form_for(@timesheet) do |f| %>
<%= f.error_messages %>
<%= f.label "Date" %>
<%= f.text_field :date %>
<% f.fields_for :runs do |builder| %>
<%= collection_select(:run, :athlete_id, Athlete.all(:order => 'name'), :id, :name, { :prompt => …Run Code Online (Sandbox Code Playgroud)