集合 Select Rails 4 中未定义的方法
Collection Select undefined method in Rails 4
我有一个表单,需要将数据库中的所有对象提取到一个 select 字段中。我已经查看了有关 collection_select 的其他 SO 问题,但似乎无法弄清楚为什么我会收到未定义的方法错误。
# Loan Application Model
class LoanApplication < ActiveRecord::Base
has_many :loan_securities, :dependent => :destroy
accepts_nested_attributes_for :loan_securities, :allow_destroy => true
end
# Loan Security Model
class LoanSecurity < ActiveRecord::Base
has_one :security_type
accepts_nested_attributes_for :security_type
end
# Security Type Model
class SecurityType < ActiveRecord::Base
belongs_to :loan_security
end
每个贷款申请都会 have_many 贷款证券,每个贷款证券都有一种证券类型。我已经为数据库植入了一些安全类型。到目前为止,该表格可以很好地处理贷款申请到贷款担保关系。
<%= nested_form_for [@business, @loanapplication], method: :put, :class => "form-horizontal", url: wizard_path, :html => { :multipart => true } do |f| %>
<%= f.fields_for :loan_securities, :wrapper => true do |loan_security| %>
<%= loan_security.collection_select(:security_type_id, SecurityType.all, :id, :name) %>
<% end %>
<% end %>
在 loanapplications_controller 中,我添加了贷款安全和安全类型的参数
loan_securities_attributes: [:id, :_destroy, security_type_attributes: [:security_type_id, :name]]
错误本身:
undefined method `security_type_id' for #<LoanSecurity:xxxxxxx>
这样做:
<%= loan_security.collection_select( :security_type_id, ::SecurityType.all, :id, :name) %>
我有一个表单,需要将数据库中的所有对象提取到一个 select 字段中。我已经查看了有关 collection_select 的其他 SO 问题,但似乎无法弄清楚为什么我会收到未定义的方法错误。
# Loan Application Model
class LoanApplication < ActiveRecord::Base
has_many :loan_securities, :dependent => :destroy
accepts_nested_attributes_for :loan_securities, :allow_destroy => true
end
# Loan Security Model
class LoanSecurity < ActiveRecord::Base
has_one :security_type
accepts_nested_attributes_for :security_type
end
# Security Type Model
class SecurityType < ActiveRecord::Base
belongs_to :loan_security
end
每个贷款申请都会 have_many 贷款证券,每个贷款证券都有一种证券类型。我已经为数据库植入了一些安全类型。到目前为止,该表格可以很好地处理贷款申请到贷款担保关系。
<%= nested_form_for [@business, @loanapplication], method: :put, :class => "form-horizontal", url: wizard_path, :html => { :multipart => true } do |f| %>
<%= f.fields_for :loan_securities, :wrapper => true do |loan_security| %>
<%= loan_security.collection_select(:security_type_id, SecurityType.all, :id, :name) %>
<% end %>
<% end %>
在 loanapplications_controller 中,我添加了贷款安全和安全类型的参数
loan_securities_attributes: [:id, :_destroy, security_type_attributes: [:security_type_id, :name]]
错误本身:
undefined method `security_type_id' for #<LoanSecurity:xxxxxxx>
这样做:
<%= loan_security.collection_select( :security_type_id, ::SecurityType.all, :id, :name) %>