简体   繁体   中英

Add multiple images by paperclip rails

This is my code in draft.rb .It is uploading single image at a time. I want to do change so that it adds multiple images using paperclip.I am new to rails.Can someone help me please.

 class Draft < ApplicationRecord
      acts_as_paranoid

      enum status: [:pending, :accepted, :rejected]
      enum action_by: [:agency, :commercial, :brand]
      belongs_to  :proposal
      belongs_to  :campaign
      belongs_to  :content
      belongs_to  :user
      belongs_to  :platform
      belongs_to  :brand
      has_one   :draft_moderation
      after_initialize :set_default_status, :if => :new_record?
      after_commit :set_draft_moderation, on: [:create, :update]

      def set_default_status
        self.status ||= :pending
      end

      def set_draft_moderation
        user_campaign_token = UserCampaignToken.where(campaign_id: self.campaign_id)
        return if (self.status != 'pending' || !user_campaign_token.present?)
        draft_moderation = DraftModeration.where(draft_id: id)
        draft_moderation.destroy_all if draft_moderation.present?
        DraftModeration.create(draft_id: id, last_moderated_at: Time.now, agency_status: 'pending')
      end

      # Draft type allowed for upload.
      def draft_type_allowed
        ['image/', 'video/']
      end

      has_attached_file :media, styles: { medium: '300x300>', thumb: '100x100>' }, if: :image?
      validates_attachment_content_type :media, content_type: %r{\Aimage\/.*\z}, if: :image?

      has_attached_file :media, if: :video?
      validates_attachment_content_type :media, content_type: %r{\Avideo\/.*\z}, if: :video?
      validates_attachment_size :media, less_than: 200.megabytes, if: :video?

      def rejection_sms
        "Dear #{user.first_name},\nLooks like you made a mistake in your campaign submission. Please check your mail to see what needs to be fixed.\nLove,\nTeam Plixxo"
      end

      def accepted_sms
        "Dear #{user.first_name},\nYour post just got approved! You are ready to take it live on your social media channel!.\nLove,\nTeam Plixxo"
      end

      def video?
        media.instance.media.instance.media_content_type =~ %r{(video)}
      end

      def image?
        media.instance.media.instance.media_content_type =~ %r{(image)}
      end
 end

You can make Attachment model and connect it to draft using has_many association.

class Attachment < ApplicationRecord
  has_attached_file :media
  ...
end

In your Draft model:

class Draft < ApplicationRecord
  has_many :attachments
end

This is just a basic structure you can add more methods to your convenience.

Useful links:

https://gist.github.com/mkornblum/1625357

https://www.cordinc.com/blog/2009/04/multiple-attachments-with-vali.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM