请帮助我了解我哪里出错了,以便在我输入所有数据后尝试保存我的数据输入表单(复杂表单)时收到此消息?
我有五个模型如下:
class Contract < AR::Base
has_many :clientlines
has_many :codelines
has_many :clients, :through => :clientlines
has_many :codes, :through => :codelines
accepts_nested_attributes_for :clientlines
end
class Clientline < AR::Base
belongs_to :contract
belongs_to :client
accepts_nested_attributes_for :contract
end
class Client < AR::Base
has_many :clientlines
has_many :contracts, :through => :clientlines
end
class Codeline < AR::Base
belongs_to :contract
belongs_to :code
units_alloc
accepts_nested_attributes_for :code
end
class Code < AR::Base
has_many :codelines
has_many :contracts, :through => :codelines
end
我使用以下文章作为我的设计来源:
http://rubysource.com/complex-rails-forms-with-nested-attributes/
在我的 app/controller/contracts_controller.rb 我有以下内容:
def new
@contract = Contract.new
4.times { @contract.codes.build }
4.times { @contract.codelines.build }
end
def create
@contract = Contract.new(params[:contract])
if @contract.save
flash[:success] = "New Contract has been saved"
redirect_to @contract # this redirects to the contract show page
else
@title = "You have some errors"
render 'new'
end
end
.
.
.
end
我把复杂的形式放在一起如下:
- provide(:title, 'Add Contract')
%h2 New Contract
=form_for(@contract) do |f|
=render 'shared/contract_error_messages', object: f.object
=render 'fields', f: f
.actions
= f.submit "Save", class: 'save strong round'
部分字段:
<fieldset><legend>Enter Contract Details</legend>
.field
= f.label :name, "AuthNum"
%br/
= f.text_field :authnum, :size => 10, :class => "ui-state-default"
.field
= f.label :name, "Start Date"
%br/
= f.text_field :st_date, :size => 12, :class => "ui-state-default"
.field
= f.label :name, "End Date"
%br/
= f.text_field :end_date, :size => 12, :class => "ui-state-default"
</fieldset>
<fieldset><legend>Enter Client Details</legend>
= f.fields_for :clients do |ff|
.field
= ff.label :name, "First Name"
%br/
= ff.text_field :f_name, :size => 15, :class => "ui-state-default"
.field
= ff.label :name, "MI"
%br/
= ff.text_field :mi, :size => 3, :class => "ui-state-default"
.field
= ff.label :name, "Last Name"
%br/
= ff.text_field :l_name, :size => 15, :class => "ui-state-default"
.field
= ff.label :name, "Birth Date"
%br/
= ff.text_field :birth_date, :size => 12, :class => "ui-state-default"
.field
= ff.label :name, "Address1"
%br/
= ff.text_field :address1, :size => 25, :class => "ui-state-default"
.field
= ff.label :name, "Address2"
%br/
= ff.text_field :address2, :size => 25, :class => "ui-state-default"
.field
= ff.label :name, "City"
%br/
= ff.text_field :city, :size => 15, :class => "ui-state-default"
.field
= ff.label :name, "ZipCode"
%br/
= ff.text_field :zip_code, :size => 10, :class => "ui-state-default"
.field
= ff.label :name, "State"
%br/
= ff.text_field :state, :size => 15, :class => "ui-state-default"
.field
= ff.label :name, "MedicareNum"
%br/
= ff.text_field :medicarenum, :size => 20, :class => "ui-state-default"
.field
= ff.label :name, "MedicaidNum"
%br/
= ff.text_field :medicaidnum, :size => 20, :class => "ui-state-default"
.field
= ff.label :name, "MemberNum"
%br/
= ff.text_field :membernum, :size => 20, :class => "ui-state-default"
.field
= ff.label :name, "SocSerCareMgr"
%br/
= ff.text_field :socsercaremgr, :size => 20, :class => "ui-state-default"
.field
= ff.label :name, "SSCM_Ph"
%br/
= ff.text_field :sscm_ph, :size => 15, :class => "ui-state-default"
.field
= ff.label :name, "NurseCareMgr"
%br/
= ff.text_field :nursecaremgr, :size => 20, :class => "ui-state-default"
.field
= ff.label :name, "NCM_Ph"
%br/
= ff.text_field :ncm_ph, :size => 15, :class => "ui-state-default"
.field
= ff.label :name, "EmergencyContact"
%br/
= ff.text_field :emergencycontact, :size => 20, :class => "ui-state-default"
.field
= ff.label :name, "EC_Ph"
%br/
= ff.text_field :ec_ph, :size => 15, :class => "ui-state-default"
.field
= ff.label :name, "PrimaryCarePhy"
%br/
= ff.text_field :primarycarephy, :size => 20, :class => "ui-state-default"
.field
= ff.label :name, "PCPhy_Ph"
%br/
= ff.text_field :pcphy_ph, :size => 15, :class => "ui-state-default"
</fieldset>
<fieldset><legend>Enter Billing Code Details</legend>
= f.fields_for :codes do |ff|
.field
= ff.label :name, "Code Name"
%br/
= ff.text_field :code_name, :size => 15, :class => "ui-state-default"
.field
= ff.label :name, "Status"
%br/
= ff.text_field :status, :size => 10, :class => "ui-state-default"
.field
= ff.label :name, "Description"
%br/
= ff.text_field :description, :size => 25, :class => "ui-state-default"
= f.fields_for :codelines do |ff|
.field
= ff.label :name, "Units Alloc"
%br/
= ff.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10, :class =>
"ui-state-default"
</fieldset>
我的第一个也是直接的问题是,一旦我在表单上输入了所有数据,然后按下“保存”按钮,我会得到以下信息:
ContractsController#Create 中的 ActiveRecord::Association TypeMisMatch。需要客户端(#xxxxxx),得到数组(#xxxxxx)。
另一个问题是,如果我在合同模型中包含“accepts_nested_attributes_for :codelines”,那么“units_alloc”属性就会从我的表单中消失。
对于这两个问题,我们将不胜感激任何帮助或指导。我花了一些时间阅读复杂表单,观看“复杂表单”railcasts,并且我已经阅读并重新阅读了有关关联的 Rails 指南以及有关 accept_nested_attributes_for 方法的 API 文档。显然,我对这些概念的理解还没有达到解决这些问题所需的全面理解,因此我寻求帮助。
更新 app/controllers/contracts_controller.rb
class ContractsController < ApplicationController
def index
@contracts = Contract.paginate(page: params[:page])
end
def show
@contract = Contract.find(params[:id])
end
def new
@contract = Contract.new
@contract.codes.build
@contract.codelines.build
@contract.clients.build
end
def create
raise params[:contract].to_s ------ **this is line #19**
@contract = Contract.new(params[:contract])
if @contract.save
flash[:success] = "New Contract has been saved"
redirect_to @contract # this redirects to the contract show page
else
@title = "You have some errors"
render 'new'
end
end
def edit
@contract = Contract.find(param[:id])
end
def update
if @contract.update_attributes(params[:contract])
flash[:success] = "Contract Profile updated"
redirect_to @contract
else
render 'edit'
end
end
end
我在contracts_controller.rb的创建操作中添加了“raise params[:contract].to_s”作为第一行,输出如下:
RuntimeError in ContractsController#create
{"authnum"=>"700900", "st_date"=>"04/03/2012", "end_date"=>"06/29/2012", "clients"=>
{"f_name"=>"Lefty", "mi"=>"L", "l_name"=>"Right", "birth_date"=>"07/18/1979",
"address1"=>"54 Frosty Lane", "address2"=>"", "city"=>"Frave", "zip_code"=>"54806",
"state"=>"WI", "medicarenum"=>"789987456", "medicaidnum"=>"931579135",
"membernum"=>"890333-3", "socsercaremgr"=>"Caring Serving",
"sscm_ph"=>"1-444-444-4444", "nursecaremgr"=>"Caring Nurse",
"ncm_ph"=>"1-555-555-5555", "emergencycontact"=>"Quick Response",
"ec_ph"=>"1-666-666-6666", "primarycarephy"=>"This One", "pcphy_ph"=>"1-777-777-7777"},
"codes"=>{"code_name"=>"S-5463", "status"=>"Active", "description"=>"Transition from
sch to mkt"}, "codelines"=>{"units_alloc"=>"80.00"}}
Rails.root: /home/tom/rails_projects/tracking
Application Trace | Framework Trace | Full Trace
app/controllers/contracts_controller.rb:19:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"/i21h2vwzuDPjIrCXzYEIAg41FnMxfGdCQQggjqcZjY=",
"contract"=>{"authnum"=>"700900",
"st_date"=>"04/03/2012",
"end_date"=>"06/29/2012",
"clients"=>{"f_name"=>"Lefty",
"mi"=>"L",
"l_name"=>"Right",
"birth_date"=>"07/18/1979",
"address1"=>"54 Frosty Lane",
"address2"=>"",
"city"=>"Frave",
"zip_code"=>"54806",
"state"=>"WI",
"medicarenum"=>"789987456",
"medicaidnum"=>"931579135",
"membernum"=>"890333-3",
"socsercaremgr"=>"Caring Serving",
"sscm_ph"=>"1-444-444-4444",
"nursecaremgr"=>"Caring Nurse",
"ncm_ph"=>"1-555-555-5555",
"emergencycontact"=>"Quick Response",
"ec_ph"=>"1-666-666-6666",
"primarycarephy"=>"This One",
"pcphy_ph"=>"1-777-777-7777"},
"codes"=>{"code_name"=>"S-5463",
"status"=>"Active",
"description"=>"Transition from sch to mkt"},
"codelines"=>{"units_alloc"=>"80.00"}},
"commit"=>"Save"}
更新 1
我将contracts_controller 新操作更改为:
def new
@contract = Contract.new
通过合约构建 codelines 对象,然后通过 codelines 对象构建代码
codelines = @contract.codelines.build
codelines.codes.build
通过合约构建clientlines对象,然后通过clientlines对象构建客户端
clientlines = @contract.clientlines.build
clientlines.clients.build
end
我还通过添加 accept_nested_attributes_for :clientlines, :codelines 以及添加 attr_accessor 行来更改我的合约模型。
class Contract < ActiveRecord::Base
has_many :clientlines
has_many :codelines
has_many :clients, :through => :clientlines
has_many :codes, :through => :codelines
accepts_nested_attributes_for :clients
accepts_nested_attributes_for :codes
accepts_nested_attributes_for :clientlines
accepts_nested_attributes_for :codelines
attr_accessor :codes, :clients, :clientlines, :codelines
end
我现在在我的参数中有带有关联名称的 nested_attribute_writers 但现在我的错误已更改为:
NoMethodError in ContractsController#new
undefined method `build' for nil:NilClass
现在的问题是,“让 attr_accessor 引用所有关联是否正确?” 我的另一个问题是,“我必须使用 form_helper 来为客户端、代码和代码行创建记录吗?” 我问这个的原因是,尽管我在contracts_controller 中有构建操作,但它似乎仍然为零。如果第二个问题的答案是肯定的,你能指导我找到一些可以指导我构建 form_helper 的资源吗?我正在检查 RailsGuides。
谢谢。
更新 2
我一直在兜圈子,我将contracts_controller 的新操作更改为:
def new
@contract = Contract.new
@contract.codes.build
@contract.clients.build
end
我还将我的合同模型改回:
class Contract < ActiveRecord::Base
has_many :clientlines
has_many :codelines
has_many :clients, :through => :clientlines
has_many :codes, :through => :codelines
accepts_nested_attributes_for :clients
accepts_nested_attributes_for :codes
attr_accessible :clients_attributes, :codes_attributes etc
end
最后,我删除了部分视图中与代码行有关的部分 _fields.html.haml,即:
= f.fields_for :codelines do |ff| and the next four lines
现在我的参数有必要的“clients_attributes”和“codes_attributes”,因此表单保存到适当的表中。我仍然有一些问题,即代码行中的额外属性,'units_alloc' 和其他一些,但事情看起来更好。