【问题标题】:Mongoid 5.0 - Rails 4 - Collection Form has_manyMongoid 5.0 - Rails 4 - 集合表单 has_many
【发布时间】:2015-09-22 13:44:16
【问题描述】:

在 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'

【问题讨论】:

  • unity_id 在哪里?你能显示代码吗?
  • 我没有创建。我认为没有必要。我必须这样做吗?
  • 现在可以了!!!!当我宣布 unity_id 时,它成功了……
  • 我总是必须声明foreign_key吗?我看到了一些例子,但没有看到。
  • 团结是典范?它属于区域还是线?

标签: ruby-on-rails ruby ruby-on-rails-4 mongoid


【解决方案1】:

您的表单有问题:

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

【讨论】:

  • 谢谢你!你是对的。我混淆了关系。我是个新手。大声笑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 2016-08-23
  • 1970-01-01
相关资源
最近更新 更多