Wrong number of Arguments ( 3 for 2 ) in Rails in f.select

爱⌒轻易说出口 提交于 2019-12-25 03:36:12

问题


I have this multiple select list which gives me an error Wrong number of Arguments ( 3 for 2 ) in a form_form a "Topic" model which has_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"} %>

However this code works just fine in another controller's view :

Here we have a Question model with has_and_belongs_to_many :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"}%>

Here is the complete _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>

CODE FIXED :

Apparently the join model had code :

belongs_to :curriculum , :grade,  :topic

Changing to the code fixed everything.

  belongs_to :curriculum
  belongs_to :grade
  belongs_to :topic

回答1:


Why not use collection_select?

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

When used in form_for parameters are as follows

first parameter is the name of the field you wish to record the value to (e.g. :curriculum_id) second parameter is the model you wish to collect items from (e.g. Curriculum.all) third parameter is the field you want to record the value from (e.g. :id) fourth parameter is the field you wish to display as the value (e.g. :name)

so for your example

<%= f.collection_select :curriculum_id, Curriculum.all, :id, :name, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>

should approximate what you want, though you may need to tweak it.



来源:https://stackoverflow.com/questions/15650991/wrong-number-of-arguments-3-for-2-in-rails-in-f-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!