加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 活动管理员有一个多态形式

发布时间:2020-12-17 02:06:14 所属栏目:百科 来源:网络整理
导读:我正在尝试设置一个选择菜单,将 ImageGallery与产品相关联. ImageGallery是多态的,因为它在几个模型之间共享. Formtastic似乎对该怎么做非常困惑.它试图在产品模型上调用名为Galleryable的方法,这是我的多态关联的名称,_id(galleryable_id). 产品 class Prod
我正在尝试设置一个选择菜单,将 ImageGallery与产品相关联. ImageGallery是多态的,因为它在几个模型之间共享. Formtastic似乎对该怎么做非常困惑.它试图在产品模型上调用名为Galleryable的方法,这是我的多态关联的名称,_id(galleryable_id).

产品

class Product < ActiveRecord::Base
  has_one :image_gallery,as: :galleryable,dependent: :destroy
  accepts_nested_attributes_for :image_gallery,:allow_destroy => true
end

画廊

class ImageGallery < ActiveRecord::Base
  belongs_to :galleryable,polymorphic: true

  validates :title,presence: true

  has_many :images,as: :imageable,dependent: :destroy
  accepts_nested_attributes_for :images,:allow_destroy => true,reject_if: lambda { |t| t['file'].nil? }

end

活动管理员表单

form do |f|
    f.inputs "Details" do
      f.input :name
      f.input :category
      f.input :price
      f.input :purchase_path
      f.input :video_panels
      f.input :image_panels
      f.input :image_gallery,:as => :select,collection: ImageGallery.all,value_method: :id
    end
    f.inputs "Image",:for => [:image,f.object.image || Image.new] do |i|
      i.input :title
      i.input :file,:as => :file,required: false,:hint => i.template.image_tag(i.object.file.url(:thumb))
    end
    f.actions
  end

我在模型上定义了galleryable_id,但这会尝试使用当然不存在的属性更新产品.

有没有人成功设置过这个?

谢谢,

科里

解决方法

我很惊讶没有人回答,因为这是一个非常有趣的场景.

你几乎得到了它但你在你的AA形式中错误地嵌套了你的关系.以下应该工作:

form do |f|
  f.inputs "Details" do
    f.input :name
    f.input :category
    f.input :price
    f.input :purchase_path
    f.input :video_panels
    f.input :image_panels
    f.inputs "ImageGallery",:for => [:image_gallery,f.object.image_gallery || ImageGallery.new] do |gallery|
      gallery.has_many :images do |image|
        image.input :title
        image.input :file,:hint => image.template.image_tag(image.object.file.url(:thumb))
      end
    end
  end
  f.actions
end

这会将“ImageGallery”与您的产品联系起来. has_one关系不能直接传递给您的父模型(正如您尝试使用f.input:image_gallery).

希望它有帮助:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读