Почему я получаю эту ошибку (недопустимый параметр :: doctor_ids)?

#ruby-on-rails

#ruby-on-rails

Вопрос:

Здесь я заполняю форму приема пациентов

  • сведения о пациентах
  • выпадающий список докторов (предопределенных)
  • обновление атрибутов модели ассоциации (:time)

но это выбрасывает недопустимый параметр : :doctor_ids. Почему?

Я искал проблему в Интернете и пробовал любой возможный способ, но все равно получаю ошибку. Также, если я делаю это, не разрешая appointments_attributes: он работает нормально.

Пожалуйста, игнорируйте написание «назначения».

 class Doctor < ApplicationRecord
#attributes - name, address
    has_many :appoinments, dependent: :destroy
    has_many :patients, through: :appoinments, dependent: :destroy
end
  
 class Patient < ApplicationRecord
#attributes - name, mobile
    has_many :appoinments, dependent: :destroy
    has_many :doctors, through: :appoinments
    accepts_nested_attributes_for :appoinments

    def associate_doctor(doctor)
        if doctor
            self.doctors << doctor
        else 
            redirect_to 'patients/new'
        end
    end
end
  
 class Appoinment < ApplicationRecord
  belongs_to :doctor
  belongs_to :patient
end
  

patient/new.html.erb

 <h2>Patient form</h2>
<%= form_for @patient do |f| %>
<b><%= f.label :name %></b><br>
<%= f.text_field :name %><br>
<b><%= f.label :mobile %></b><br>
<%= f.text_field :mobile %><br>

<b>Select Doctor for Appoinment:</b><br>
<%= f.select :doctor_ids, Doctor.order(:name).map{|a| [a.name, a.id]}, include_blank: "Select" %><br>

<b>Appoinment time</b><br>
<%= f.fields_for :appoinments, @patient.appoinments.build do |a|%>
<b><%= a.label :time %></b>
<%= a.text_field :time %><br>
  <% end %>
<%= f.submit %>
<% end %>
  

Контроллер пациентов

 def create
    #debugger
    @patient = Patient.create(patient_params)
    if @patient.save
        doctor = Doctor.find_by(id: params[:patient][:doctor_ids])
        @patient.associate_doctor(doctor)
        redirect_to @patient
    else
        render 'new'
    end
  end

private

  def patient_params
    params.require(:patient).permit(:name, :mobile, doctor_ids: [:id, :name], appoinments_attributes: [:time])
  end
  

Комментарии:

1. Удалить doctor_ids из patient_params , поскольку вы назначаете его create .

2. Если у вас есть время, переместите redirect_to из модели.

3. да, я уже удалил его. Спасибо