Mongoid 5.0 - Rails 4 - 收集表格 has_many

Mongoid 5.0 - Rails 4 - Collection Form has_many

在 mongoid

中使用 has_many 时无法获取表单工作宽度集合

模型线:

class Line
  include Mongoid::Document
  include Mongoid::Timestamps

  field :observations
  field :position, :type => Integer
  field :status, :type => Integer

  has_many :unities, :inverse_of => :unity
end

模型统一:

class Unity
  include Mongoid::Document
  include Mongoid::Timestamps      
  field :prefix, type: Integer
  field :owner_name
  field :owner_email
  field :owner_phone
  field :document      
  field :license
  field :color
  field :active, type: Mongoid::Boolean, default: false
  field :qrx, type: Mongoid::Boolean, default: false
  belongs_to :line, index: true
end

我的表格是:

<%= bootstrap_form_for @line do |f| %>
    <div class="col-md-2">      
        <%= f.collection_select :unity_id, Unity.all, :id, :title %>
    </div>
<% end %>

我收到此错误:#

的未定义方法“unity_id”

您的表格有问题:

<%= bootstrap_form_for @line do |f| %>
    <div class="col-md-2">      
        <%= f.collection_select :unity_id, Unity.all, :id, :title %>
    </div>
<% end %>

@line 有很多单元,它没有 unity_id 字段。您也没有在 Line 模型中定义 title 字段。

也许你可以这样做:

<%= bootstrap_form_for @unity do |f| %>
    <div class="col-md-2">      
        <%= f.collection_select :line_id, Line.all, :id, :owner_name %>
    </div>
<% end %>