【问题标题】:Activeadmin polymorphic association, paperclip attachmentsActiveadmin 多态关联,回形针附件
【发布时间】:2016-07-31 12:59:04
【问题描述】:

我正在为 car 资源使用 activeadmin,并且多个附件没有进入记录,car 记录已成功创建,但在创建时它不包含附件。我有两个模型“附件”,models/attachment.rb

class Attachment < ActiveRecord::Base
    belongs_to :imageable, polymorphic: true

    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" },default_url: "/images/:style/missing.png"
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end

并且 我的model/car.rb 包含以下代码

class Car < ActiveRecord::Base
   has_many :attachments, as: :imageable
   accepts_nested_attributes_for :attachments
end

在我的app/admin/car.rb 中,我有以下用于多个附件的代码。

form do |f|
  f.input :make
  f.input :model
  f.input :color
  f.input :engine_type
  f.input :description
  f.has_many :attachments do |attachment|
    attachment.input :attachment, :as => :file
  end
  f.actions
end

谁能解释一下如何解决这个问题?

【问题讨论】:

  • 可以为您的admin/car.rb 显示您的 permit_params 吗?
  • 当您尝试保存/创建记录时,日志文件说明了什么?

标签: ruby-on-rails paperclip activeadmin


【解决方案1】:

我认为您的输入字段应该是 :avatar 而不是 :attachment

所以,它应该看起来像

f.has_many :attachments do |attachment|
  attachment.input :avatar, :as => :file
end

【讨论】:

  • 表单也需要多部分form multipart: true do |f|
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
相关资源
最近更新 更多