0

I am creating instructors page which have multiple course ids to be passed to the model parameter list.

The following is my code for whitelist params

def instructor_params
  params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, {:courses_ids => []})
end

My create page is like below

<%= form_for @instructor, :html => { :class => "form-horizontal instructor" } do |f| %>
    <% if @instructor.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@instructor.errors.count, "error") %> prohibited this instructor from being saved:</h2>

          <ul>
            <% @instructor.errors.full_messages.each do |message| %>
                <li><%= message %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

    <div class="form-group">
      <%= f.label :LastName, :class => 'control-label col-md-2' %>
      <div class="col-md-10">
        <%= f.text_field :LastName, :class => 'form-control' %>
        <%= error_span(@instructor[:LastName]) %>
      </div>

    </div>
    <div class="form-group">
      <%= f.label :FirstMidName, :class => 'control-label col-md-2' %>
      <div class="col-md-10">
        <%= f.text_field :FirstMidName, :class => 'form-control' %>
        <%= error_span(@instructor[:FirstMidName]) %>
      </div>

    </div>
    <div class="form-group">
      <%= f.label :HireDate, :class => 'control-label col-md-2' %>
      <div class="col-md-10">
        <%= f.text_field :HireDate, :class => 'form-control' %>
        <%= error_span(@instructor[:HireDate]) %>
      </div>
    </div>

    <div class="form-group">
      <%= f.label text="Courses", :class => 'control-label col-md-2' %>
      <div class="col-md-10">
        <table cellpadding="5">
          <tr>
            <td>
              <input type="checkbox" name="course_ids" value="Course1">Course1
              <input type="checkbox" name="course_ids" value="Course2">Course2
              <input type="checkbox" name="course_ids" value="Course3">Course3
            </td>
          </tr>
        </table>
      </div>
    </div>


    <div class="form-group">
      <div class="col-md-offset-2 col-md-10">
        <%= f.submit nil, :class => 'btn btn-primary' %>
        <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                    instructors_path, :class => 'btn btn-default' %>
      </div>
    </div>
<% end %>

But if select multiple check-boxes params not passing as arrays. ?It took only the last one and showing not permitted. Please guide me. I don't have any attribute named course_ids in Instructor model.

Akhil
  • 1,918
  • 5
  • 30
  • 74

1 Answers1

0

To receive an array of values, you need to change the name of your checkboxes to be course_ids[]

In your permitted params, {:courses_ids => []} can be written simply as course_ids: [] (also notice the typo you had with the plural 'courses').

deefour
  • 34,974
  • 7
  • 97
  • 90
  • thanks it worked but how i created entries in referenced table along with main table insertion – Akhil Apr 22 '15 at 04:05
  • That's a whole separate question, outside the scope of this one. I suggest reading up on [`accepts_nested_attributes_for`](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for) both in the Rails docs and here on StackOverflow. – deefour Apr 22 '15 at 04:07
  • ok i will try that and post another question if any problem exists. – Akhil Apr 22 '15 at 04:08