Ruby On Rails : Unpermitted parameters with has many
<p>Hello everyone,
I've been searching through stackoverflow but didn't find an answer yet so i'm asking directly.</p>

<p>I have a a simple table named message which contains a user id, and a content. I have now a second and a third table which contains the message_id from the message table, a recipient_id and a visibility.</p>

<p>When i submit the form, i always have the same error : Unpermitted parameters: message_recipients, message_groups. Even if i add them in the permit...</p>

<p>Let me show you :</p>

<p><strong>Message.rb</strong></p>

<pre><code>class Message &lt; ActiveRecord::Base
  include Tenantable

  belongs_to :user
  has_many :message_recipients
  has_many :message_groups

  validates :content, :user, presence: true

  default_scope { order(created_at: :desc) }
end
</code></pre>

<p><strong>message_recipient.rb</strong></p>

<pre><code>class MessageRecipient &lt; ActiveRecord::Base
  belongs_to :user
  belongs_to :message
end
</code></pre>

<p><strong>message_group.rb</strong></p>

<pre><code>class MessageGroup &lt; ActiveRecord::Base
  belongs_to :user_group
  belongs_to :message
end
</code></pre>

<p><strong>messages_controller.rb</strong></p>

<pre><code>class MessagesController &lt; ApplicationController
  # GET /messages/new
  def new
    @message = Message.new
  end

  # POST /messages
  def create
    @message = Message.new(message_params)
    @message.user = current_user

    respond_to do |format|
      if @message.message_recipients.empty?
        format.html { render :new}
      else
        if @message.save
          @message.message_recipients.user = current_user
          @message.message_recipients.message = @message

          format.html { redirect_to authenticated_root_path, notice: t('messages.created') }
        else
          format.html { render :new }
        end
      end
    end
  end

  private

  # Never trust parameters from the scary internet, only allow the white list through.
  def message_params
    params.require(:message).permit(:content, { message_recipient_ids: [], message_group_ids: [] })
  end
end
</code></pre>

<p><strong>_form.html.erb</strong></p>

<pre><code>&lt;% if @message.errors.any? %&gt;
&lt;div class="alert alert-danger" role="alert"&gt;
  &lt;h4&gt;&lt;span class="glyphicon glyphicon-alert" aria-hidden="true"&gt;&lt;/span&gt; &lt;%= t('activerecord.errors.record_invalid', count: @message.errors.count) %&gt;&lt;/h4&gt;
  &lt;ul&gt;
  &lt;% @message.errors.full_messages.each do |message| %&gt;
    &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
  &lt;% end %&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;% end %&gt;
&lt;%= form_for(@message, html: { class: 'form-horizontal' }) do |f| %&gt;
    &lt;div class="form-group"&gt;
      &lt;%= f.label :message_recipients, class: 'col-sm-2 control-label' %&gt;
      &lt;div class="col-sm-3"&gt;
        &lt;%= f.select :message_recipients, options_for_select(User.all.map { |s|[s.firstname + ' ' + s.lastname, s.id] }) %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;%= f.label :message_groups, class: 'col-sm-2 control-label' %&gt;
      &lt;div class="col-sm-3"&gt;
        &lt;%= f.select :message_groups, options_for_select(UserGroup.all.map { |s|[s.name, s.id] }) %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
   &lt;div class="form-group"&gt;
    &lt;%= f.label :content, class: 'col-sm-2 control-label' %&gt;
    &lt;div class="col-sm-3"&gt;
      &lt;%= f.text_area :content, class: 'form-control' %&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="form-group"&gt;
    &lt;div class="col-sm-3 col-sm-offset-2"&gt;
      &lt;button type="submit" class="btn btn-primary"&gt;&lt;%= t('actions.save') %&gt;&lt;/button&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;% end %&gt;
</code></pre>