1

我有一个表单,需要将数据库中的所有对象都拉到一个选择字段中。我已经查看了有关 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

每个贷款申请都将有_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>
4

1 回答 1

1

做这个:

<%= loan_security.collection_select( :security_type_id, ::SecurityType.all, :id, :name) %>
于 2015-03-04T20:31:16.297 回答