Rails has_many :through nested form

前端 未结 3 1489
孤独总比滥情好
孤独总比滥情好 2020-12-04 15:58

I have just jumped into has_many :through association. I\'m trying to implement the ability to save data for all 3 tables (Physician, Patient

相关标签:
3条回答
  • 2020-12-04 16:16

    "i got it working. I just changed models as follows" : quoted from Shruti in the comments above

    class Patient < ActiveRecord::Base 
      has_many :appointments, :dependent => :destroy 
      has_many :physicians, :through => :appointments
      accepts_nested_attributes_for :appointments
    end 
    
    class Appointment < ActiveRecord::Base
      belongs_to :physician 
      belongs_to :patient
      accepts_nested_attributes_for :physician
    end
    
    0 讨论(0)
  • 2020-12-04 16:18

    Ok, this little bugger of a question stumped me for a few hours, so I'm going to post my working solution on here in hopes it shaves some time for peeps. This is for Rails 4.0 and Ruby 2.0. This also overcame a "symbol to integer conversion" issue I had.

    Models:

    class Patient < ActiveRecord::Base 
      has_many :appointments
      has_many :physicians, :through: :appointments
      accepts_nested_attributes_for :appointments
    end 
    
    class Appointment < ActiveRecord::Base
      belongs_to :physician 
      belongs_to :patient
      accepts_nested_attributes_for :physician
    end
    
    class Physicians < ActiveRecord::Base
      has_many :appointments
      has_many :patients, through: :appointments
    end
    

    Controller:

    def new
      @patient= Patient.new 
      @appointments = @patient.appointments.build
      @physician = @appointments.build_physician 
    end
    
    def create
      Patient.new(patient_params)
    end
    
    
    def patient_params
       params.require(:patient).permit(:id, appointments_attributes: [:id, :appointment_time, physician_attributes: [:id ] )
    end
    

    View

    <% form_for(@patient) do |patient_form| %>
      <%= patient_form.error_messages %>
      <p>
        <%= patient_form.label :name, "Patient Name" %>
        <%= patient_form.text_field :name %>
      </p>
    
      <% patient_form.fields_for :appointments do |appointment_form| %>
        <p>
          <%= appointment_form.label :appointment_date, "Appointment Date" %>
          <%= appointment_form.date_field :appointment_date %>
        </p>
    
        <% appointment_form.fields_for :physician do |physician_form| %>
          <p>
            <%= physician_form.label :name, "Physician Name" %>
            <%= physician_form.text_field :name %>
          </p>
        <% end %>
      <% end %>
    
      <p>
        <%= patient_form.submit 'Create' %>
      </p>
    <% end %>
    
    <%= link_to 'Back', patients_path %>
    
    0 讨论(0)
  • 2020-12-04 16:22

    Your patient class accepts nested attributes for both physicians and appointments. Try adding another fields_for method for appointment.

    <% form_for(@patient) do |patient_form| %>
      <%= patient_form.error_messages %>
      <p>
        <%= patient_form.label :name, "Patient Name" %>
        <%= patient_form.text_field :name %>
      </p>
    
      <% patient_form.fields_for :physicians do |physician_form| %>
        <p>
          <%= physician_form.label :name, "Physician Name" %>
          <%= physician_form.text_field :name %>
        </p>
      <% end %>
    
      <% patient_form.fields_for :appointments do |appointment_form| %>
        <p>
          <%= appointment_form.label :appointment_date, "Appointment Date" %>
          <%= appointment_form.date_field :appointment_date %>
        </p>
      <% end %>
    
      <p>
        <%= patient_form.submit 'Create' %>
      </p>
    <% end %>
    
    <%= link_to 'Back', patients_path %>
    
    0 讨论(0)
提交回复
热议问题