Ruby on rails 使用Mongoid对轨道进行分组

Ruby on rails 使用Mongoid对轨道进行分组,ruby-on-rails,ruby,mongoid,mongoid3,Ruby On Rails,Ruby,Mongoid,Mongoid3,我对使用Mongoid的分组集合选择有问题。我收到的错误消息是:ActionView::Template::Error(未定义的方法“merge”for:name:Symbol) 我使用的是rails 3.2.12&最新版本的Mongoid 这是我的密码: class States include Mongoid::Document field :_id field :name field :abbreviation field :countryId belongs_t

我对使用Mongoid的分组集合选择有问题。我收到的错误消息是:ActionView::Template::Error(未定义的方法“merge”for:name:Symbol)

我使用的是rails 3.2.12&最新版本的Mongoid

这是我的密码:

class States
  include Mongoid::Document
  field :_id
  field :name
  field :abbreviation
  field :countryId

  belongs_to :countries
end

class Countries
  include Mongoid::Document
  field :_id
  field :name
  field :abbreviation

  has_many :states
end

<div class="field"> 
  <%= f.label :_id, "Country" %><br />
  <%= f.collection_select :_id, Countries.order_by([:name, :asc]), :_id, :name, include_blank: true %>
</div>
<div class="field">
  <%= f.label :_id, "State or Province" %><br />
  <%= f.grouped_collection_select(:states, :countryId, Countries.order_by([:name, :asc]), :states, :name, :id, :name) %>
</div>
类状态
include Mongoid::Document
字段:\u id
字段:名称
字段:缩写
字段:countryId
属于:国家
结束
阶级国家
include Mongoid::Document
字段:\u id
字段:名称
字段:缩写
有很多州吗
结束



如果您能为解决此问题提供任何帮助,我将不胜感激。

您正在使用
f.grouped\u collection\u select
,这意味着第一个参数将从
f.object
中推断出来。删除第一个参数并将第二个参数更改为要保存该参数的字段的列名应该可以解决问题

<%= f.grouped_collection_select(:state, Countries.order_by([:name, :asc]), :states, :name, :id, :name) %>


感谢您的快速回复。我实现了您的更改,但现在收到以下错误消息:ActionView::Template::error(未定义#的“state”方法)。有没有关于如何解决的想法?任何帮助都将不胜感激。您需要在我对要保存所选状态的字段名的回答中更改
:state
。如果不知道您在哪里使用表单,我们将不知道字段名是什么。您好,jvnill,您能否提供一个小示例,说明保存所选状态时字段名是什么?谢谢你在哪里用这个表格?什么型号的?选中该模型并将
:state
更改为要保存所选状态的列名。您好,首先,感谢您的帮助!我正在使用以下模型:class Equipment include Mongoid::Document include Mongoid::Timestamps字段:description字段:modelNumber字段:title字段:categoryId字段:subCategoryId字段:state_id end我想将状态id保存为state_id。这是我调整后的控制器控件:我现在得到一个未初始化的常量状态错误。任何帮助都将不胜感激。请尝试以下操作: