1
resposta

Problema com nested form has_many through utilizando formulario dinamico

Estou com o seguinte problema em um formulário utilizando has_many through.

Tenho um cadastro onde eu criou formulários dinamicos. Esses formulários dinamicos são inseridos em uma outra tela.

Estou com dificuldade no controller onde eu insiro esses fomulários e na view que faço a inserção.

Appointments -> onde posso ter muito formularios dinamicos (record_forms) Record_Forms -> cadastro dos formulários dinamicos Record_Form_Fields -> campos do formulario dinamico Appointment_Record_Forms -> tabela para relacionamento entre Appointments e Record_Forms

Schema

 create_table "appointment_record_forms", force: :cascade do |t|
   t.integer "appointment_id", limit: 4 
   t.integer "record_form_id", limit: 4 
   t.datetime "created_at", null: false 
   t.datetime "updated_at", null: false t.text "properties", limit: 65535 
end

create_table "appointments", force: :cascade do |t|
   t.string "duration", limit: 255 t.date "appointment_date" 
   t.integer "patient_id", limit: 4 
   t.integer "doctor_id", limit: 4 t.datetime "created_at", null: false 
   t.datetime "updated_at", null: false 
end

create_table "record_form_fields", force: :cascade do |t| 
   t.string "name", limit: 255 t.string "field_type", limit: 255 
   t.boolean "required" t.integer "record_form_id", limit: 4 
   t.datetime "created_at", null: false 
   t.datetime "updated_at", null: false 
end

create_table "record_forms", force: :cascade do |t| 
   t.string "name", limit: 255 t.datetime "created_at", null: false 
   t.datetime "updated_at", null: false 
end

Models

class Appointment < ActiveRecord::Base 
   belongs_to :patient belongs_to :doctor
   has_many :appointment_record_forms, dependent: :destroy 
   has_many :record_forms, through: :appointment_record_forms 
   has_many :record_form_fields

   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

Controller

class AppointmentsController < ApplicationController

   def appointment_params   
      params.require(:appointment).permit(:duration, :appointment_date, :patient_id, :doctor_id, :record_form_ids, :appointment_form_ids ) 
      end
end

View

Appointments/_form.html.erb

<%= f.fields_for :appointment_record_forms do |builder| %> 
   <%= f.select_tag :record_form_id, options_from_collection_for_select(RecordForm.all, :id, :name) %> 
   <%= render 'appointment_record_form_fields', f: builder %> 
<% end %> 
<%= link_to_add_fields "Add Field", f, :appointment_record_forms %>

Partial

Appointments/_appointment_record_form_fields.html.br

<%= 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" %>

Atualmente ao rodar a aplicação esta com o erro:

undefined method `properties' for :appointment_record_forms:Symbol

Gostaria de saber como eu devo fazer no controller do appointment para receber a associação, na view para inserir o formulario dinamico e Partial para listar salvar os registro no hash e mostrar os campos do formulario corretamente.

Desde de já agradeço se alguem poder ajudar.

1 resposta

Opa Vinicius, com as informações que você deu, eu suspeito que no partial o @appointmenet_record_forms seja um Symbol e não o objeto que você realmente esperava.

Para conseguir te ajudar melhor, consegue postar a stacktrace completa? Assim vai dar pra ver a exata linha de código onde o erro acontece.

Abraços