【问题标题】:Grouped Collection Select分组集合选择
【发布时间】:2013-08-05 14:07:49
【问题描述】:

我在其中一个表单中遇到了group_collection_select 的一些问题。

这就是我收到的错误信息:

undefined method `assert_valid_keys' for :company:Symbol

我已经解决了一段时间的问题,但我就是无法解决这个问题。

我的grouped_collection_code 看起来像这样:

<%= f.grouped_collection_select :subsector_id, Sector.all, :subsectors, :sector_name, :id, :subsector_name %>

我的模型如下所示:

class Sector < ActiveRecord::Base
  attr_accessible :sector_name
  has_many :companies
  has_many :subsectors
end

class Subsector < ActiveRecord::Base
  attr_accessible :sector_id, :subsector_name, :subsector_id
  belongs_to :sector, :company
end

class Company < ActiveRecord::Base
  belongs_to :sector
  has_many :subsectors, through: :sectors
end

我不知道这是否有帮助,但我为表单提供的 javascript 如下所示:

jQuery ->
  subsectors = $('#company_subsector_id').html()
  $('#company_sector_id').change ->
    sector = $('#company_sector_id :selected').text()
    options = $(subsectors).filter("optgroup[label='#{sector}']").html()
    if options
      $('#company_subsector_id').html(options)
      $('#company_subsector_id').parent().show()
    else
      $('#company_subsector_id').empty()
      $('#company_subsector_id').parent().hide()

您能否提供帮助或指导我如何解决此错误?

【问题讨论】:

    标签: javascript ruby-on-rails forms grouped-collection-select


    【解决方案1】:

    您的belongs_to 声明导致了此问题。 belongs_to 声明中不能有多个名称。每个关联都需要单独定义。请将其更改为:

    # Class Subsector < ActiveRecord::Base
    
    belongs_to :sector
    belongs_to :company
    

    在此处查看 belongs_to 的文档:http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to

    【讨论】:

    • 什么是简单的错误。我没有足够的经验来捕捉那些但我猜......谢谢你的帮助。
    • @user2543601,这类问题很容易被忽视:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多