我按照这个截屏视频制作了一个嵌套模型表单。 http://railscasts.com/episodes/196-nested-model-form-part-1
现在,我正在尝试在我的应用程序中包含这样一个嵌套模型表单,但我收到了一个参数错误(参数数量错误(0 代表 1))。
我似乎无法弄清楚我哪里出错了,我想就我可以尝试什么以及为什么可能会发生一些错误寻求一些建议。
错误发生在主题模型中的这一行。
has_many :lessons, :dependent => destroy
其他相关代码:
主题控制器:
def new
@subject = Subject.new
#3 times one for lecture one for lab one for tut.
3.times{@subject.lessons.build}
respond_to do |format|
format.html # new.html.erb
format.json { render json: @subject }
format.js
end
end
主题模型
class Subject < ActiveRecord::Base
has_many :lessons, :dependent => destroy
attr_accessible :lesson_attributes, :acad_unit, :cohort_size, :discipline, :remarks, :subject_code, :subject_name, :year_of_study
accepts_nested_attributes_for :lessons, :reject_if => lambda { |a| a[:lesson_type].blank? }, :allow_destroy => true
end
课程模型
class Lesson < ActiveRecord::Base
belongs_to :subject
attr_accessible :frequency, :lesson_type, :no_of_lesson, :possible_venues
end
_form.html.erb
<%= form_for(@subject,:remote=>true) do |f| %>
<% if @subject.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@subject.errors.count, "error") %> prohibited this subject from being saved:</h2>
<ul>
<% @subject.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :subject_code %><br />
<%= f.text_field :subject_code %>
</div>
<div class="field">
<%= f.label :subject_name %><br />
<%= f.text_field :subject_name %>
</div>
<div class="field">
<%= f.label :year_of_study %><br />
<%= f.text_field :year_of_study %>
</div>
<div class="field">
<%= f.label :discipline %><br />
<%= f.text_field :discipline %>
</div>
<div class="field">
<%= f.label :acad_unit %><br />
<%= f.text_field :acad_unit %>
</div>
<div class="field">
<%= f.label :cohort_size %><br />
<%= f.text_field :cohort_size %>
</div>
<div class="field">
<%= f.label :remarks %><br />
<%= f.text_field :remarks %>
</div>
<ol>
<%= f.fields_for :lessons do |builder| %>
<%= render "lesson_fields", :f => builder %>
<% end %>
</ol>
<% end %>
_lesson_fields.html.erb
<p>
<div class="field">
<%= f.label :lesson_type %><br />
<%= f.text_field :lesson_type %>
</div>
<div class="field">
<%= f.label :no_of_lesson %><br />
<%= f.text_field :no_of_lesson %>
</div>
<div class="field">
<%= f.label :frequency %><br />
<%= f.text_field :frequency %>
</div>
<div class="field">
<%= f.label :possible_venues %><br />
<%= f.text_field :possible_venues %>
</div>
</p>