Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby on Rails应用程序上的AssociationTypeMismatch错误_Ruby On Rails - Fatal编程技术网

Ruby on rails Ruby on Rails应用程序上的AssociationTypeMismatch错误

Ruby on rails Ruby on Rails应用程序上的AssociationTypeMismatch错误,ruby-on-rails,Ruby On Rails,我的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 :mid

我的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

我是RoR的新手,希望通过实践学习该语言。我想知道代码可能有什么问题。谢谢

首先,您的
患者
模型需要一个
接受

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
您的视图有一个小错误。对于地址,它需要是
字段,而对于@address
,它不需要是
字段。另外,由于控制器现在是RESTful的,您可以删除
: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 -%>


30%>
30%>
30%>
病人的永久地址




希望这有帮助:)

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