fix: Resolve QR code generation errors in ticket PDF creation

- Add validation in TicketPdfGenerator to ensure QR code data integrity
- Use compact() to remove nil values from QR code data hash
- Add error handling in Ticket#generate_qr_code with fallback generation
- Validate QR code data before passing to RQRCode library
- Add proper error logging for QR code generation failures
- Prevent "data must be a String, QRSegment, or an Array" errors

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
kbe
2025-09-04 01:43:40 +02:00
parent ec5095d372
commit 5fc790cd42
2 changed files with 17 additions and 3 deletions

View File

@@ -63,6 +63,10 @@ class Ticket < ApplicationRecord
self.qr_code = SecureRandom.uuid self.qr_code = SecureRandom.uuid
break unless Ticket.exists?(qr_code: qr_code) break unless Ticket.exists?(qr_code: qr_code)
end end
rescue => e
Rails.logger.error "Failed to generate QR code for ticket: #{e.message}"
# Generate a simple fallback QR code
self.qr_code = "#{id || 'temp'}-#{Time.current.to_i}-#{SecureRandom.hex(4)}"
end end

View File

@@ -63,12 +63,22 @@ class TicketPdfGenerator
pdf.text "Ticket QR Code", align: :center pdf.text "Ticket QR Code", align: :center
pdf.move_down 10 pdf.move_down 10
# Ensure all required data is present before generating QR code
if ticket.qr_code.blank?
raise "Ticket QR code is missing"
end
qr_code_data = { qr_code_data = {
ticket_id: ticket.id, ticket_id: ticket.id,
qr_code: ticket.qr_code, qr_code: ticket.qr_code,
event_id: ticket.event.id, event_id: ticket.event&.id,
user_id: ticket.user.id user_id: ticket.user&.id
}.to_json }.compact.to_json
# Validate QR code data before creating QR code
if qr_code_data.blank? || qr_code_data == "{}"
raise "QR code data is empty or invalid"
end
qrcode = RQRCode::QRCode.new(qr_code_data) qrcode = RQRCode::QRCode.new(qr_code_data)
pdf.print_qr_code(qrcode, extent: 120, align: :center) pdf.print_qr_code(qrcode, extent: 120, align: :center)