【发布时间】: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_attachments 的 record_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