Ruby on rails 4 Rails,通过嵌套表单创建具有不同列类型的多个关联条目
我的应用程序有以下型号:Ruby on rails 4 Rails,通过嵌套表单创建具有不同列类型的多个关联条目,ruby-on-rails-4,nested-forms,Ruby On Rails 4,Nested Forms,我的应用程序有以下型号: Advertisment (has_many :images) Image (belongs_to :advertisment) Image Fields: advertisment_id, image_type, image_url 应用程序1中有两种图像类型。正方形和2。横幅 我希望能够为两种类型的图像创建一个带有两个浏览按钮的嵌套表单,并使用两种类型的图像创建一个广告 预期结果: #<Advertisment id: 1, name: "Test", co
Advertisment (has_many :images)
Image (belongs_to :advertisment)
Image Fields: advertisment_id, image_type, image_url
应用程序1中有两种图像类型。正方形和2。横幅
我希望能够为两种类型的图像创建一个带有两个浏览按钮的嵌套表单,并使用两种类型的图像创建一个广告
预期结果:
#<Advertisment id: 1, name: "Test", content: "Lorem ipsum", created_at: "2014-08-01 20:14:06", updated_at: "2014-08-01 20:14:06">
[#<Image id: 1, advertisment_id: 1, image_url: "/images/test1.png", image_type: "banner", created_at: "2014-08-01 20:14:09", updated_at: "2014-08-01 20:14:09">, <Image id: 2, advertisment_id: 1, image_url: "/images/test2.png", image_type: "square" ,created_at: "2014-08-01 20:14:26", updated_at: "2014-08-01 20:14:26">]
#
[#, ]
我需要尽快得到一些指导。提前谢谢 为什么不为每个图像创建两个字段,如
#<Image id: 1, advertisment_id: 1, banner_url: "/images/test1.png", square_url: "/images/test2.png", created_at: "2014-08-01 20:14:09", updated_at: "2014-08-01 20:14:09">
因此,如我前面所述,使用gem,这样您就可以使用@ad.image.image\u url
访问横幅,并使用@ad.image.image\u url.square
进行平方。同样,我不明白为什么您只想在图像模型中使用一个图像url列。在这种情况下,使用banner\u-url
和square\u-url
选项进行操作非常有意义(当然是这样的):)
class ImageUploader < CarrierWave::Uploader::Base
...
process :resize_to_fit => [700, 400] # for banner
version :square do
process resize_to_fill: [300, 300] # for square
end
end