1
votes

I want to create an appointment form with dynamics forms. But I'm not getting what I'm doing wrong

I'm having trouble with the partial _appointment_record_form_fields and with appointment_controller.

Someone can help me fix the controller and the view?

My Models:

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

My Controller

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

Views: views/appointment/_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

Hi and welcome to Stack overflow. Thanks heaps for adding your code. Can you clarify a bit about the problem you are having. "I'm having trouble" is a bit vague. What do you expect to see? what do you see instead? what have you tried to make it work and what other research have you done? (we don't want to duplicate your efforts) - Taryn East
Hi, when I create a new appointment the app should allow I enter multiple dynamic forms (record_forms) and display its fields (record_form_fields). The values for the dynamics fields should be salve in the hash properties. I'm having trouble with the partial _appointment_record_form_fields, with appointment_controller and with _appointment_record_form_fields thats is not salving and showing correctly the dynamic forms. - vinicius carvalho
Now I'm getting the error ActionView::Template::Error (undefined method `properties' for nil:NilClass): - vinicius carvalho
" is not salving and showing correctly the dynamic forms" You need to edit your question (don't post code here in the comments because code formatting is dreadful) and add an example of this. Show us what information you entered. Show us the server-logs that have the params that arrived at the controller. show us what is displaying in the forms, and then tell us what you'd expect to see in the forms. - Taryn East