1

我正在构建一个 Rails 引擎。我有以下两个模型类:

module LandingPageEng
    class LandingPage < ActiveRecord::Base
        attr_protected # just for debugging right now
        has_many :slide_show_images, :dependent => :destroy
        accepts_nested_attributes_for :slide_show_images, allow_destroy: true
    end
end

第二类是:

module LandingPageEng
    class SlideShowImage < ActiveRecord::Base
        attr_accessible :image, :landing_page_id
        belongs_to :landing_page 

        validates :image, :presence => true
    end
end

与它们关联的表是landing_page_eng_landing_page 和landing_page_eng_slide_show_image。

当我在控制台中运行以下命令时,我收到错误 NameError: uninitialized constant SlideShowImage。

1.9.3-p194 :001 > LandingPageEng::LandingPage.new({"title"=>"wd", "tagline"=>"wed",     "slide_show_images"=>{"_destroy"=>""}})
NameError: uninitialized constant SlideShowImage
    from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-  3.2.6/lib/active_support/inflector/methods.rb:229:in `block in constantize'
    from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:228:in `each'
    from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:228:in `constantize'
    from /Users/martinjlogan/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
<snip>

我一直在努力解决这个问题,无法弄清楚。非常感谢任何帮助。

4

1 回答 1

0

我没有带有引擎的 Rails 3 设置来快速测试它,但我认为问题出在您的has_many配置上;它正在寻找一个名为SlideShowImage但你的类名为LandingPageEng::SlideShowImage.

我相信:class_name为您添加一个选项has_many将解决此问题。

http://railsapi.com/doc/rails-v3.0.8rc1/classes/ActiveRecord/Associations/ClassMethods.html#M004956

于 2012-06-13T05:18:39.313 回答