Paperclip不是为一个模型存储替代尺寸,而是为另一个模型存储

时间:2015-12-07 09:53:41

标签: ruby-on-rails paperclip

我有两个附有图像的模型。第一个定义如下:

class Post < ActiveRecord::Base
  has_attached_file :cover_image, styles: { large: "750x750^", medium: "600x600>", thumb: "100x100>" }, default_url: ":style/missing.gif"
  validates_attachment_content_type :cover_image, content_type: /\Aimage\/.*\Z/
end

这非常有效,并且在我/公共文件夹中总共存储了4张图像。

我的第二个模特虽然......

class Lesson < ActiveRecord::Base
  has_attached_file :image, styles: { large: "750x750^", medium: "600x600>", thumb: "100x100>" }, default_url: ":style/missing.gif"
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

...仅将原始图像存储在名为original的文件夹中。

我的服务器日志确实在POST请求中显示了这样的内容:

Command :: file -b --mime '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-jxf7m7.jpg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]'
Command :: convert '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]' -auto-orient -resize "750x750^" '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/a802cd21677cc830b7ba993a8ff7edd920151207-39937-1p9h15m'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]'
Command :: convert '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]' -auto-orient -resize "600x600>" '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/a802cd21677cc830b7ba993a8ff7edd920151207-39937-r4bxgl'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]' 2>/dev/null
Command :: identify -format %m '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]'
Command :: convert '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-wmm770.jpg[0]' -auto-orient -resize "100x100>" '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/a802cd21677cc830b7ba993a8ff7edd920151207-39937-4zvubq'
Command :: file -b --mime '/var/folders/2b/q6z4f1fn58s83fbx77xwsk5h0000gn/T/64ecce8dbf7049731a63696ae4d7933020151207-39937-19vrckc.jpg'

这可能是由字段的名称引起的吗?还有其他想法吗?

修改 我的课程模型实际上有点复杂,因为它使用共享表来区分内容类型:

class Lesson < ActiveRecord::Base
  self.table_name = 'steps'
  default_scope { where(content_type: name) }
  has_attached_file :cover_image, styles: { large: "750x750^", medium: "600x600>", thumb: "100x100>" }, default_url: ":style/missing.gif"
  validates_attachment_content_type :cover_image, content_type: /\Aimage\/.*\Z/

  validates :title, presence: true, length: { maximum: 255 }

  def self.create_for_course(course, attributes = {})
    content = new(attributes)
    if content.valid?
      step = Step.create(content_type: name, course: course)
      instance = step.becomes(self)
      instance.update_attributes(attributes)
      instance
    else
      step = content.becomes(Step)
      step.course = course
      step.content_type = name
      step.becomes(self)
    end
  end
  #include Content
  #restrict_attrs :title, :content_body, :cover_image_file_name, :cover_image_content_type, :cover_image_file_size, :cover_image_updated_at, :cover_image

end

1 个答案:

答案 0 :(得分:1)

评论太久了;您将从使用paperclip_defaults选项中受益:

#app/models/post.rb
class Post < ActiveRecord::Base
  has_attached_file :cover_image
  validates_attachment_content_type :cover_image, content_type: /\Aimage\/.*\Z/
end

这样您就可以在模型中调用附件名称,如果您愿意,可以覆盖默认值:

#app/models/asset.rb
class Asset < ActiveRecord::Base
    #Associations
    ##################
    belongs_to :assetable, polymorphic: true

    #Paperclip
    ##################
    delegate :url, to: :attachment

########################################

    ##################
    #     Methods    #
    ##################
    before_create :dimensions #-> https://github.com/thoughtbot/paperclip/wiki/Extracting-image-dimensions

    #Width & Height
    def dimensions
        return unless image? && has_dimensions?
        self.width  = geometry.width
        self.height = geometry.height
    end

    ########

    private

    #Geometry -> https://github.com/galetahub/ckeditor/blob/master/lib/ckeditor/backend/paperclip.rb#L23
    def geometry
        @geometry ||= begin
            file = attachment.respond_to?(:queued_for_write) ? attachment.queued_for_write[:original] : attachment.to_file
            ::Paperclip::Geometry.from_file(file)
        end
    end

    #Image?
    def image?
        image_types = %w(image/jpeg image/png image/gif image/jpg image/pjpeg image/tiff image/x-png)
        image_types.include? attachment_content_type
    end

    #Dimensions
    def has_dimensions?
        respond_to?(:width) && respond_to?(:height)
    end
end

-

关于您的错误,我不确定问题可能是什么。

我有一个建议。这有点棘手,但是如果你能让它发挥作用,它会使你的所有模型都变得干燥。

Here's a link

#app/models/post.rb
class Post < ActiveRecord::Base
    has_one :cover, class_name: "Post::Cover", as: :assetable, dependent: :destroy
    accepts_nested_attributes_for :cover, reject_if: :all_blank
   has
end

#app/models/post/cover.rb
class Post::Cover < Asset
   has_attached_file :attachment
end

资产表: enter image description here

然后可以与STI一起使用:

cover

这允许您将assets属性设置为嵌套关联,但是还可以在create_for_course表中存储应用程序中的所有资源:

enter image description here

-

<强>更新

您可以使用以下方法制作#app/models/post.rb class Lesson < ActiveRecord::Base attr_reader :course belongs_to :step before_create :set_step, if Proc.new { |a| a.course } private def set_step self.step.create(course: course) #-> we can use the course virtual attribute) end end 方法DRYer:

lesson = Lesson.create course: "5"
# automatically creates "step" with course of "5" and current lesson

虽然我怀疑它是否具体捕获了您的功能,但 允许您运行:

{{1}}