【问题标题】:Rails Active Storage not working every time. Some time it works, sometime it doesn'tRails Active Storage 并非每次都工作。有时有效,有时无效
【发布时间】:2021-08-27 05:36:23
【问题描述】:

我正在使用 Active Storage 存储匹配分数的图像。我的课是这样的

class TournamentMatch < ApplicationRecords
  has_many_attached :score_cards
end

我的记分卡控制器如下所示

class Public::ScoreCardsController < Public::BaseController
  def new
    session[:tournament_match_id] = params[:tournament_match_id]
  end

  def create
    tournament_match_id = session[:tournament_match_id]
    tournament_id = session[:tournament_id]
    session[:tournament_match_id] = nil
    session[:tournament_id] = nil

    tournament_match = TournamentMatch.find_by(id: tournament_match_id)

    if tournament_match.score_cards.attach(params['score_cards'])
      flash[:notice] = 'Score card uploaded'
    else
      flash[:error] = 'Score card upload failed'
    end
      
    redirect_to public_results_path(tournament_id: "#{tournament_id}/", anchor: 'results')
  end

  def view
    @tournament_match = TournamentMatch.find_by(id: params[:tournament_match_id])
    render layout: "application"
  end
end

所以在我的创建操作中,我通过 id 找到 TournamentMatch,然后将记分卡附加到它。我每次上传都使用相同的图像文件。但有时图像会附加,有时则不会。 tournament_match.score_cards.attach(params['score_cards'] 总是在控制器中返回 true。但是如果我找到相同的锦标赛匹配并在控制台中运行tournament_match.score_cards.attach(params['score_cards'],那么它会返回错误。我还注意到,当没有保存图像时,active_storage_attachmentsrecord_id 设置为 0。但如果图像被保存,则record_id 的值大于 0。

我已经没有调试的想法了。请建议我如何调试此问题。

更新:以下查询来自日志文件,它显示record_id 为 nil

ActiveStorage::Attachment Exists? (0.6ms)  SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", nil], ["record_type", "TournamentMatch"], ["name", "score_cards"], ["LIMIT", 1]]

【问题讨论】:

    标签: ruby-on-rails rails-activestorage


    【解决方案1】:

    我的主要问题是我使用:uuid 作为我所有表的主键,所以我必须编辑 ActiveStorage 迁移以使用:uuid。我主要更改了t.references :record, null: false, polymorphic: true, index: false, type: :uuidt.references :blob, null: false, type: :uuid 这两条线。我的最终迁移看起来像这样。在此之后 ActiveStorage 现在可以正常工作了。

    class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
      def change
        create_table :active_storage_blobs, id: :uuid do |t|
          t.string   :key,        null: false
          t.string   :filename,   null: false
          t.string   :content_type
          t.text     :metadata
          t.bigint   :byte_size,  null: false
          t.string   :checksum,   null: false
          t.datetime :created_at, null: false
    
          t.index [ :key ], unique: true
        end
    
        create_table :active_storage_attachments, id: :uuid do |t|
          t.string     :name,     null: false
          t.references :record,   null: false, polymorphic: true, index: false, type: :uuid
          t.references :blob,     null: false, type: :uuid
    
          t.datetime :created_at, null: false
    
          t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
          t.foreign_key :active_storage_blobs, column: :blob_id
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多