Rails 写真用小モデル追加

マイグレーションファイル作成

carrywave でuploaderを作成

uploaderの名前は自由に決めれる

rails generate uploader Avatar

モデルに記載する

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

モデルmeeting.rbに紐付けるのでモデルでネストする

  # meeting pictures
  has_many :meeting_pictures
  accepts_nested_attributes_for(
    :meeting_pictures,
    reject_if: :picture_blank?,
    allow_destroy: true
  )
  def picture_blank?(attributes)
    #picture_cacheはcarrywaveの機能
    attributes['picture'] ||= attributes['picture_cache']
    return attributes['picture'].blank?
  end  

meeting controller

   def meeting_params
      # params.fetch(:meeting, {})
      params.require(:meeting).permit(
        :id,
        :title,
        :date,
        :content,
        :client_id,
        :user_id,
        meeting_distributions_attributes:[
          :id,
          :distribution_id,
          :meeting_id,
          :_destroy
        ],
        meeting_pictures_attributes:[
          :id,
          :picture,
          :picture_cache,
          :_destroy
        ]
      )
    end