我有这个多重选择列表,它给了我一个错误错误数量的参数(3 对 2)在 form_form 一个“主题”模型,它有 _many :curriculums, :through => another_model
<%= f.select :curriculum_ids, options_from_collection_for_select(Curriculum.all, :id, :name, @topic.curriculum_ids), {}, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>
然而,这段代码在另一个控制器的视图中工作得很好:
这里我们有一个带有 has_and_belongs_to_many 的 Question 模型:skills
<%=f.select :skill_ids, options_from_collection_for_select(Skill.all, :id, :name, @question.skill_ids),{},{"data-placeholder"=>"Select Other Skills",:multiple=>true,:class=>"chzn-select"}%>
这是完整的 _form.html.erb :
<div class="row-fluid attributeContainer">
<div class="span12">
<% if @topic.errors.any? %>
<% @topic.errors.full_messages.each do |msg| %>
<div class="alert alert-error">
<button class="close" data-dismiss="alert">×</button>
<strong>Error!</strong> <%= msg %>
</div>
<% end %>
<% end %>
<div class="widget-box">
<div class="widget-title">
<span class="icon">
<i class="icon-align-justify"></i>
</span>
<h5>Topics</h5>
</div>
<div class="widget-content nopadding">
<%= form_for(@topic,:html=>{:class=>"form-horizontal"}) do |f| %>
<div class="control-topic">
<label class="control-label">Topic Name</label>
<div class="controls">
<%= f.text_field :name %>
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<textarea name="topic[description]" value="<%=@topic.description%>"><%=@topic.description%></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label">Subject</label>
<div class="controls">
<div class="span3">
<%= f.collection_select(:subject_id, Subject.all, :id, :name, {:include_blank => 'Please Select Subject'}) %>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Curriculum</label>
<div class="controls">
<div class="span3">
<%= f.select :curriculum_ids, options_from_collection_for_select(Curriculum.all, :id, :name, @topic.curriculum_ids), {}, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>
</div>
</div>
</div>
<div class="control-group attributeTemplate2" style="display:none;">
<label class="control-label">Concept Tag</label>
<div class="controls" >
<div class="span3" >
<%= f.collection_select(:name, Concept.all,:id, :name,{:include_blank => 'Please Select'},{:name=>"topic[concept][][concept_id]"}) %>
</div>
<div class="span2">
<%= f.text_field :name, :style=> "margin-left:25px;", :class=>'text_field', :name=>'topic[concept][][concept_sequence]', :placeholder=>'concept_sequence', :size=>'30'%>
</div>
<div>
<a style="margin-left:25px;" class="btn btn-danger removeRow"><i class="icon-white icon-remove-sign"></i></a>
</div>
</div>
</div>
<div class="control-group ">
<label class="control-label">Concept Tag</label>
<div class="controls" >
<div class="span3" >
<%= f.collection_select(:name, Concept.all,:id, :name,{:include_blank => 'Please Select'},{:name=>"topic[concept][][concept_id]",:class=>"chzn-select"}) %>
</div>
<div class="span2">
<%= f.text_field :name, :style=> "margin-left:25px;", :class=>'text_field', :name=>'topic[concept][][concept_sequence]', :placeholder=>'concept_sequence', :size=>'30'%>
</div>
<div>
<a style="margin-left:25px;" class="btn btn-danger removeRow"><i class="icon-white icon-remove-sign"></i></a>
</div>
</div>
</div>
<div class="control-group addTopicConcepts">
<div class="controls" >
<div><a class="btn btn-primary">Add Concept</a></div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
</div>
<%end%>
</div>
</div>
</div>
</div>
代码固定:
显然连接模型有代码:
belongs_to :curriculum , :grade, :topic
更改为代码修复了一切。
belongs_to :curriculum
belongs_to :grade
belongs_to :topic