denisov_2023
@denisov_2023 (デニソフ)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

carrierwave_backgrounderで画像が投稿されない

□解決したいこと

railsでcarrierwave_backgrounderを用いて画像投稿を非同期処理を行おうと思っております。

しかし、投稿自体はできるのですが、画像のアップロードができません。

挙動のGIF
https://gyazo.com/f8578e73cff5e1e38c5b020b9ebe4411

以下、実装したコードの内容

model/tip.rb

class Tip < ApplicationRecord
  belongs_to :user
  has_many :comments
  mount_uploader :image, ImageUploader # carrierwave
  store_in_background :image
  has_many :tip_tag_relations, foreign_key: :tip_id, dependent: :destroy
  has_many :tags, through: :tip_tag_relations
  has_many :likes
  has_many :liked_users, through: :likes, source: :user
  <省略>
  end
end
model/tip_tag.rb

class TipTag
  include ActiveModel::Model
  attr_accessor :title, :category_id, :description, :user_id, :image, :name, :id, :_method, :authenticity_token, :commit, :tip

  validates :category_id, numericality: { other_than: 1 }
  with_options presence: true do
    validates :title, :description, presence: true
  end

  delegate :persisted?, to: :tip

  def initialize(attributes = nil, tip: Tip.new)
    @tip = tip
    attributes ||= default_attributes
    super(attributes)
  end

  def save(tag_list)
    ActiveRecord::Base.transaction do
      @tip.update(title: title, category_id: category_id, description: description, image: image, user_id: user_id)
      @tip.tip_tag_relations.each do |tag|
        tag.delete
      end

      tag_list.each do |tag_name|
        tag = Tag.where(name: tag_name).first_or_initialize
        tag.save
        tip_tag = TipTagRelation.where(tip_id: @tip.id, tag_id: tag.id).first_or_initialize
        tip_tag.update(tip_id: @tip.id, tag_id: tag.id)
      end
    end
  end

  def to_model
    tip
  end

  private

  def default_attributes
    {
      title: tip.title,
      category_id: tip.category_id,
      description: tip.description,
      image: tip.image,
      name: tip.tags.pluck(:name).join(','),
    }
  end
end
uploaders/image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  if Rails.env.development?
    storage :file
    include ::CarrierWave::Backgrounder::Delay
  elsif Rails.env.test?
    storage :file
  else
    storage :fog
    include ::CarrierWave::Backgrounder::Delay
  end

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    # "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    if Rails.env.test?
      "uploads_#{Rails.env}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    else
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  end

  # Create different versions of your uploaded files:
  version :thumb do
    process convert: ['png', 0]
    process resize_to_limit: [1000, 1000]
  end
end
workers/test_worker.rb

class TestWorker
  include Sidekiq::Worker
  sidekiq_options queue: :test
  def perform(tip_params, tag_list)
    tip = TipTag.new(tip_params)
    tip.save(tag_list)
  end
end
CarrierWave::Backgrounder.configure do |c|
  # c.backend :delayed_job, queue: :carrierwave
  # c.backend :active_job, queue: :carrierwave
  # c.backend :resque, queue: :carrierwave
  c.backend :sidekiq, queue: :carrierwave
  # c.backend :girl_friday, queue: :carrierwave
  # c.backend :sucker_punch, queue: :carrierwave
  # c.backend :qu, queue: :carrierwave
  # c.backend :qc
end
config/initializers/sidekiq.rb

Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://localhost:6379', namespace: 'test_sidekiq' }
end

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://localhost:6379', namespace: 'test_sidekiq' }
end
:verbose: false
:pidfile: ./tmp/pids/sidekiq.pid
:logfile: ./log/sidekiq.log
:concurrency: 10
:queues:
  - [carrierwave, 1]
  - default
  - test
$ bundle exec sidekiq -q test -C config/sidekiq.yml
<省略> 
2021-07-22T10:02:11.334Z pid=62815 tid=ov4fn7ua3 class=TestWorker jid=fa5acb575e34b7981317d8c8 INFO: start
2021-07-22T10:02:11.468Z pid=62815 tid=ov4fn7ua3 class=TestWorker jid=fa5acb575e34b7981317d8c8 elapsed=0.134 INFO: done
$ bundle exec sidekiq -q carrierwave -C config/sidekiq.yml
<省略> 
2021-07-22T10:02:11.468Z pid=62893 tid=oupmco9wd class=CarrierWave::Workers::StoreAsset jid=28bc5caf5e0090ceee4286d3 INFO: start
2021-07-22T10:02:11.471Z pid=62893 tid=oupmco9wd class=CarrierWave::Workers::StoreAsset jid=28bc5caf5e0090ceee4286d3 elapsed=0.004 INFO: done
$ redis server
<省略> 
63359:C 22 Jul 2021 19:07:10.110 * DB saved on disk
58356:M 22 Jul 2021 19:07:10.203 * Background saving terminated with success

実装は以下の記事を参考に行いました。

https://workabroad.jp/posts/1205

データベースにも画像の情報が反映されておりませんでした。

悩んだ末に何が原因なのかも仮定することができませんでした。

どこが原因なのか、お分かりの方がいらっしゃいましたらご教示いただきたくお願い申し上げます。

0

No Answers yet.

Your answer might help someone💌