2009-07-23 16 views
9

Tengo problemas con mi aplicación Ruby on Rails. Tengo dos modelos: 'paciente' y 'dirección', un paciente tiene una dirección y una dirección pertenece a un paciente.AssociationTypeMismatch Error en la aplicación Ruby on Rails

Patient.rb

class Patient < ActiveRecord::Base 
    has_many :charge_slips 
    has_one :address 

    validates_presence_of :last_name 
    validates_presence_of :first_name 
    validates_presence_of :middle_name 

end 

Address.rb

class Address < ActiveRecord::Base 
    belongs_to :patient 
    validates_associated :patient 
end 

Paciente-controller.rb

class PatientController < ApplicationController 
    def index 
    @title = "Outpatient Services - Patient" 
    @today = Date.today.to_formatted_s(:long) 
    @patients = Patient.find(:all) 
    end 

    def new 
    @patient = Patient.new 
    @address = Address.new 
    end 

    def create 
    @patient = Patient.new(params[:patient]) 
    @patient.created_on = Date.today.to_formatted_s(:long) 

    if @patient.save 
     @address = Address.new(params[:address]) 
     @address.patient_id = @patient.id 
     if @address.save 
     redirect_to :action => 'index' 
     else 
     redirect_to :action => 'new' 
     end 
     redirect_to :action => 'index' 
    else 
     redirect_to :action => 'new' 
    end 
    end 
end 

new.html.rb

<%= content_tag('h3', 'Create New Patient') %> 
<hr> 
<% form_for @patient, :url => { :action => "create" } do |patient_form| -%> 
    <%= error_messages_for :patient %> 
    <%= patient_form.label :last_name, 'Last Name:' %> <%= patient_form.text_field :last_name, :size => 30 %><br> 
    <%= patient_form.label :first_name, 'First Name:' %> <%= patient_form.text_field :first_name, :size => 30 %><br> 
    <%= patient_form.label :middle_name, 'Middle Name:' %> <%= patient_form.text_field :middle_name, :size => 30 %><br> 

    <fieldset> 
     <legend>Patient's Permanent Address</legend> 
     <%= error_messages_for :address %> 
     <% patient_form.fields_for @address do |address_fields| -%> 
      <%= address_fields.label :street_name, 'Street Name:' %> <%= address_fields.text_field :street_name %><br> 
      <%= address_fields.label :barangay, 'Barangay:' %> <%= address_fields.text_field :barangay %><br> 
      <%= address_fields.label :city_municipality, 'City/Municipality:' %> <%= address_fields.text_field :city_municipality %><br> 
      <%= address_fields.label :country, 'Country:' %> <%= address_fields.text_field :country %><br> 
      <%= address_fields.label :zip_cide, 'Zip Code:' %> <%= address_fields.text_field :zip_code %><br> 
     <% end -%> 
    </fieldset> 

    <%= submit_tag "Add Patient" %> 
<% end -%> 

Cada vez que agrego un nuevo paciente, se produce un error. Aquí está una parte del error:

ActiveRecord::AssociationTypeMismatch in PatientController#create 

Address(#31360520) expected, got HashWithIndifferentAccess(#23815500) 

RAILS_ROOT: C:/www/Outpatient Application Trace | Framework Trace | Full Trace 

C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_proxy.rb:263:in `raise_on_type_mismatch' 
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/has_one_association.rb:52:in `replace' 
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations.rb:1246:in `address=' 
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2740:in `send' 
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2740:in `attributes=' 
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2736:in `each' 
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2736:in `attributes=' 
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2434:in `initialize' 
C:/www/Outpatient/app/controllers/patient_controller.rb:14:in `new' 
C:/www/Outpatient/app/controllers/patient_controller.rb:14:in `create' 

Soy nuevo en RoR y me gustaría aprender el idioma a través de la práctica. Quiero saber qué podría estar mal con el código. ¡Gracias!

Respuesta

28

En primer lugar su modelo Patient necesita una accepts_nested_attributes_for

class Patient < ActiveRecord::Base 
    has_many :charge_slips 
    has_one :address 

    validates_presence_of :last_name 
    validates_presence_of :first_name 
    validates_presence_of :middle_name 

    accepts_nested_attributes_for :address 

end 

Su controlador se puede simplificar en gran medida. No es necesario guardar la dirección por separado, ya que @patient.save se encargará de eso. No es necesario configurar manualmente el atributo created_on ya que se configurará automágicamente :) También cuando @patient.save falla probablemente desee render :action => 'new' y no redirect_to :action => 'new'. Esto volverá a mostrar el formulario con cualquier error de validación (redirect_to will not).

También tenga en cuenta que cambié el nombre de su clase de controlador a PatientsController en lugar de PatientController. Esto estará más en línea con las convenciones RESTful de Rails y también te ayudará a simplificar un poco tu visión. Si haces esto necesitarás un map.resources :patients en tu archivo routes.db, y tendrás que cambiar el nombre de tus archivos también.

class PatientsController < ApplicationController 
    def index 
    @title = "Outpatient Services - Patient" 
    @today = Date.today.to_formatted_s(:long) 
    @patients = Patient.find(:all) 
    end 

    def new 
    @patient = Patient.new 
    @patient.build_address 
    end 

    def create 
    @patient = Patient.new(params[:patient]) 

    if @patient.save 
     redirect_to :action => 'index' 
    else 
     render :action => 'new' 
    end 
    end 
end 

Su vista tiene un pequeño error. Necesita ser fields_for :address y no fields_for @address. Además, dado que su controlador ahora es RESTful, puede eliminar la pieza :url => { :action => "create" }.

<%= content_tag('h3', 'Create New Patient') %> 
<hr> 
<% form_for @patient do |patient_form| -%> 
    <%= error_messages_for :patient %> 
    <%= patient_form.label :last_name, 'Last Name:' %> <%= patient_form.text_field :last_name, :size => 30 %><br> 
    <%= patient_form.label :first_name, 'First Name:' %> <%= patient_form.text_field :first_name, :size => 30 %><br> 
    <%= patient_form.label :middle_name, 'Middle Name:' %> <%= patient_form.text_field :middle_name, :size => 30 %><br> 

    <fieldset> 
     <legend>Patient's Permanent Address</legend> 
     <%= error_messages_for :address %> 
     <% patient_form.fields_for :address do |address_fields| -%> 
       <%= address_fields.label :street_name, 'Street Name:' %> <%= address_fields.text_field :street_name %><br> 
       <%= address_fields.label :barangay, 'Barangay:' %> <%= address_fields.text_field :barangay %><br> 
       <%= address_fields.label :city_municipality, 'City/Municipality:' %> <%= address_fields.text_field :city_municipality %><br> 
       <%= address_fields.label :country, 'Country:' %> <%= address_fields.text_field :country %><br> 
       <%= address_fields.label :zip_cide, 'Zip Code:' %> <%= address_fields.text_field :zip_code %><br> 
     <% end -%> 
    </fieldset> 

    <%= submit_tag "Add Patient" %> 
<% end -%> 

Espero que esto ayude :)