0

我的应用程序有问题。我有两对模型

首先:

          class Nazione < ActiveRecord::Base
                has_many :regiones

                accepts_nested_attributes_for :regiones
                attr_accessible :nome, :regiones_attributes
          end

         class Regione < ActiveRecord::Base
               belongs_to :nazione
               attr_accessible :nome, :nazione_id
         end

第二对:

     class Macrocategorie < ActiveRecord::Base
       has_many :categories
       accepts_nested_attributes_for :categories
       attr_accessible :nome, :categories_attributes
     end

      class Categorie < ActiveRecord::Base
         belongs_to :macrocategorie

         attr_accessible :nome, :macrocategorie_id
      end

它们用于另一个名为 Modulo1 的模型中的表单:

     class Modulo1 < ActiveRecord::Base
         attr_accessible :nazione_organizzazione,:regione_organizzazione,:macrocat_sett_scient ,:cat_sett_scient

当我使用时,以 Modulo1 的形式:

      <%= f.label :Nazione%><br />
      <%= f.collection_select :nazione_organizzazione, Nazione.order(:nome), :nome, 
                                         :nome,{:prompt => "Seleziona una Nazione"} %>

     <%= f.label :Regione %><br>
     <%= f.grouped_collection_select :regione_organizzazione, Nazione.order(:nome), 
               :regiones, :nome, :nome, :nome, {:prompt => "Seleziona una Regione"} %>

有用!但如果我使用

     <%= f.label :Settore_Scientifico %><br>
     <%= f.collection_select :macrocat_sett_scient, Macrocategorie.order(:nome), :nome, 
                                  :nome, {:prompt => "Seleziona una Macrocategoria"} %>
     <%= f.grouped_collection_select :cat_sett_scient, 
                           Macrocategorie.order(:nome),:categories,:nome, :nome,:nome,  
                                              {:prompt => "Seleziona una Categoria"} %>

它不工作!错误是这样的:

      uninitialized constant Macrocategorie::Category

谁能帮我解决这个问题?

4

1 回答 1

0

Rails 无法识别关联的另一端属于哪个类,因为您的类名不是英文的。您可以显式设置类名:

class Macrocategorie < ActiveRecord::Base
  has_many :categories, class_name: 'Categorie'
end

另一种选择是在config/intializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'categorie', 'categories'
end
于 2014-07-07T09:32:14.507 回答