ruby-on-rails – 嵌套模型表单中的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

运行嵌套在Timesheets下,我想在创建Timesheet的同一表单上创建一些运行,如此railscast所示

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

在我的时间表表单上,我遇到了我的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 => 'Select Athlete' } ) %>
  <% end %>

<%= f.submit 'Create' %>

<% end %>

是否可以使用如上所示的collection_select填充Run的:athlete_id字段,在Timesheet的嵌套表单中,或者我错过了什么?

解决方法

看起来你没有在表单构建器上创建集合选择,尝试这样的事情:
<% f.fields_for :runs do |r| %>
  <%= r.collection_select(:athlete_id,{ :prompt => 'Select Athlete' } ) %>
<% end %>

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...