Search code examples
ruby-on-railsmany-to-manyruby-on-rails-6

Rails 6: Unpermitted Parameter (many-to-many)


RAILS 6

Hey, I'm working on a class system that uses units and assignments as a many-to-many relationship. When I submit a new assignment form with a dropdown collection for units, the unit is not being received by the controller, but no error log is displayed. When I use byebug, the following error is displayed:

Unpermitted parameter: :unit_ids

Even though it has been permitted. Here's my controller for assignments.

class AssignmentsController < ApplicationController
    def new
        @assignment = Assignment.new
    end

    def create
        debugger
        @assignment = Assignment.new(assignment_params)
        @assignment.save
        if @assignment.save
            flash[:success] = "The unit was successfully submitted."
            redirect_to units_path
        else 
            render 'new'
        end        
    end

    def show

    end


    private

    def assignment_params
        params.require(:assignment).permit(:name, :description, :duedate, user_ids: [])
    end
end

Using byebug, I know the unit_id is being correctly received, from this form:

<%= form_for(@assignment, :html => {class: "form-horizontal", role: "form"}) do  |f| %>
    <div class="form-group">
        <div>
            <%= f.collection_select(:unit_ids, Unit.all, :id, :name, placeholder: "Units" )%>

        </div>

        <div>
              <%= f.text_field :name, class:"form-control", placeholder: "Title of Assignment", autofocus: true %> 
        </div>
        <div>
              <%= f.text_area :description, class:"form-control materialize-textarea", placeholder: "Assignment Description", autofocus: true %> 
        </div>
        <div>
            <%= f.text_field :duedate, class: "datepicker", placeholder: "Due Date"%>
        </div>

        <div class="form-group" id="submitbutton">
            <div align = "center">
                <%= f.submit  class: "btn waves-effect waves-light" %>
            </div>
        </div>
    </div>
<%end%>

Here are the relevant models just to be safe. Note that I added the nested lines to both after I received this error because I saw it on another thread, but it doesn't seem to be fixing it.

class Unit < ApplicationRecord
    has_and_belongs_to_many :users
    has_and_belongs_to_many :assignments
    accepts_nested_attributes_for :assignments
end

And the Assignment model:

class Assignment < ApplicationRecord
      has_and_belongs_to_many :units
      has_many :users, :through => :units
      accepts_nested_attributes_for :units
end

Solution

  • The answer was a mix of a couple things, as Rockwell pointed out I was using User instead of Units, but that still didn't fix it. My collection had multiple choices set to false, so my controller wanted simply

        params.require(:assignment).permit(:name, :description, :duedate, :unit_ids)
    

    However, when I set multiple to true, that didn't work. Then, it wanted

    params.require(:assignment).permit(:name, :description, :duedate, unit_ids[])
    

    My solution was to leave multiple as true, and use the unit_ids[].