Problem for insert record into join table rails

Viewed 74

I use rails 6, and I would like insert 2 fk in my join table.

I have this error but I don't understand why. Unpermitted parameter: :user_id a log : document"=>{"category_id"=>"1", "document_file"=>#<ActionDispatch::Http::UploadedFile:0x000000015009e538 @tempfile=#<Tempfile:/var/folders/j9/6gbqsy057wq5zlkql65gb4b80000gn/T/RackMultipart20211231-59408-dy0wip.png>, @original_filename="Capture d’écran 2021-11-09 à 14.59.03.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"document[document_file]\"; filename=\"Capture d\xE2\x80\x99e\xCC\x81cran 2021-11-09 a\xCC\x80 14.59.03.png\"\r\nContent-Type: image/png\r\n">, "document_users_attributes"=>{"0"=>{"user_id"=>["", "2"]}}}, "commit"=>"Create Document"}

I have same relations for this picture, just name table is not a same. Just into 'phyiscians' I have fk user (my table is Document) enter image description here

My models :

class Document < ApplicationRecord
  belongs_to :category
  belongs_to :user
  has_many :document_users
  delegate :username, to: :user
  has_one_attached :document_file

accepts_nested_attributes_for :document_users

class DocumentUser < ApplicationRecord
  belongs_to :user
  belongs_to :document
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable,:lockable,:trackable

has_many :documents
has_many :document_users

My controller Document :

def new
    @document = Document.new
    @documentU = @document.document_users.build

  end



def document_params
      params.require(:document).permit(:shared, :category_id,:document_file,:user_id, document_users_attributes:[:document_id,:user_id])
    end

into a form Document _form :

<%= simple_form_for @document do |f| %>
  <% if document.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(document.errors.count, "error") %> prohibited this document from being saved:</h2>

      <ul>
        <% document.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">

      <%= f.input :shared, as: :boolean, checked_value: true, unchecked_value: false %>
  </div>

  <div class="field">
    <%= f.select :category_id, Category.all.map{|s|[s.nameCategory, s.id]} %>

  </div>


  <div class="field">
    <%= f.input :document_file, as: :file %>
  </div>
<%= f.fields_for :document_users do |fd| %>
  <%= fd.label :user_id %>
  <%= fd.input :user_id, collection:User.all.map{|s|[s.username, s.id]}, input_html: { multiple: true }%>
<% end %>
0 Answers
Related