为什么会出现此错误(不允许的参数::doctor_ids)?

时间:2019-04-08 10:54:36

标签: ruby-on-rails

在这里,我正在填写患者服用的表格

  • 患者详细信息
  • 下拉医生列表(预定义)
  • 更新关联模型属性(:time)

但是这将抛出Unpermitted参数::doctor_ids。 为什么?

我在互联网上搜索了该问题,并尝试了所有可能的方法,但仍然出现错误。 另外,如果我在没有允许appoinments_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

0 个答案:

没有答案