using has_many through with dynamic forms rail 4

Ich möchte ein Terminformular mit dynamischen Formularen erstellen. Aber ich verstehe nicht, was ich falsch mache

Ich habe Probleme mit den Teilfeldern _Termin_Aufzeichnung_Formular und mit dem Terminkontroller.

Kann mir jemand helfen, den Controller und die Ansicht zu reparieren?

Meine Modelle:

class Appointment < ActiveRecord::Base

  has_many   :appointment_record_forms, dependent: :destroy
  has_many   :record_forms, through: :appointment_record_forms

 accepts_nested_attributes_for  :appointment_record_forms, allow_destroy: true

end



 class AppointmentRecordForm < ActiveRecord::Base
  belongs_to :appointment
  belongs_to :record_form


  serialize :properties, Hash

  def validate_properties
    record_form.record_form_fields.each do |record_form_field|
      if record_form_field.required? && properties[record_form_field.name].blank?
        errors.add record_form_field.name, "must not be blank"
      end
    end
  end

end

class RecordForm < ActiveRecord::Base
    has_many :record_form_fields

    has_many :appointment_record_forms, dependent: :destroy
    has_many :appointments, through: :appointment_record_forms

    accepts_nested_attributes_for :record_form_fields, allow_destroy: true

end

class RecordFormField < ActiveRecord::Base
  belongs_to :record_form
end

Meine Steuerung

class AppointmentsController < ApplicationController
.
.
.
    def appointment_params
      params.require(:appointment).permit(:duration, :appointment_date, :patient_id, :doctor_id, 
                                           :record_forms => [:id, :name, :_destroy],
                                           :record_form_fields => [:id, :name, :field_type, :require, :record_form_id, :_destroy]) 
    end
end

Ansichten: Ansichten / Termin / _Form

...
  <%= f.fields_for :appointment_record_forms do |builder| %>
    <%= render 'appointment_record_form_fields', f: builder %>
  <% end %>
  <%= link_to_add_fields "Add Field", f, :appointment_record_forms %>
...

partial / _appointment_record_form_fields

 <fieldset> 
  <%= f.fields_for :properties, OpenStruct.new(@appointment_record_forms.properties) do    |builder| %>
    <% @appointment_record_forms.record_form.record_form_fields.each do |record_form_field| %>

      <%= render "appointments/fields/#{record_form_field.field_type}", record_form_field: record_form_field, f: builder %>
    <% end %>
  <% end %>

  <%= f.hidden_field :_destroy %>
  <%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>

TKS

Antworten auf die Frage(0)

Ihre Antwort auf die Frage