Ruby on rails 无法在具有多态图像模型的carrierwave的mongoid中嵌入多个图像
我很难一起使用carrierwave mongoid。我正在编写一个需要将图像存储在多个集合中的应用程序,因此我创建了如下图像类(请原谅使用imageable):Ruby on rails 无法在具有多态图像模型的carrierwave的mongoid中嵌入多个图像,ruby-on-rails,ruby-on-rails-3,mongoid,nested-forms,carrierwave,Ruby On Rails,Ruby On Rails 3,Mongoid,Nested Forms,Carrierwave,我很难一起使用carrierwave mongoid。我正在编写一个需要将图像存储在多个集合中的应用程序,因此我创建了如下图像类(请原谅使用imageable): ImageUploader是由rails-generate-uploader-Image创建的 然后我有一个故事模型: class Story include Mongoid::Document include Mongoid::Timestamps field :title, :type => String
ImageUploader
是由rails-generate-uploader-Image
创建的
然后我有一个故事模型:
class Story
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
field :content, :type => String
field :category, :type => String
embeds_many :blog_images, as: :imageable
end
当我试图创建一个
故事
对象时,即使我没有通过表单提供和镜像,我也会得到错误未初始化常量BlogImage
。我还尝试在控制台中调用story.blog_images您需要为blog_images指定目标类,因为它不能从关系的名称派生。尝试:
embeds_many :blog_images, as: :imageable, :class_name => 'Image'
您需要为blog_图像指定目标类,因为它不能从关系的名称派生。尝试:
embeds_many :blog_images, as: :imageable, :class_name => 'Image'
这解决了我描述的问题。此外,我想指出,该行需要读取embedded\u many:blog\u images,as::imageable,:class\u name=>'Image',cascade\u回调:true
,因此解决了我描述的问题。此外,我想指出的是,该行需要读取embedded\u many:blog\u images,as::imageable,:class\u name=>“Image”,cascade\u回调:true
,因为
embeds_many :blog_images, as: :imageable, :class_name => 'Image'