From 360529830eba4cdff634120341cb9860c186b060 Mon Sep 17 00:00:00 2001 From: Martin Ortbauer Date: Fri, 16 Feb 2024 17:01:35 -0500 Subject: [PATCH] fix active storage links for attachments --- app/controllers/application_controller.rb | 5 +++++ app/views/admin/users/_form.html.haml | 2 +- app/views/admin/users/show.html.haml | 2 +- app/views/finance/invoices/_form.html.haml | 2 +- app/views/finance/invoices/show.html.haml | 2 +- app/views/finance/invoices/unpaid.html.haml | 2 +- 6 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3537f8c4b..56a2c311a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,6 +11,11 @@ class ApplicationController < ActionController::Base after_action :remove_controller around_action :set_time_zone, :set_currency + # needed for linking to attachments on locals storage + before_action do + ActiveStorage::Current.url_options = { protocol: request.protocol, host: request.host, port: request.port } + end + # Returns the controller handling the current request. def self.current Thread.current[:application_controller] diff --git a/app/views/admin/users/_form.html.haml b/app/views/admin/users/_form.html.haml index 6bd4859b6..f7013f66f 100644 --- a/app/views/admin/users/_form.html.haml +++ b/app/views/admin/users/_form.html.haml @@ -8,7 +8,7 @@ - f.object.attachments.reject(&:new_record?).each do |attachment| .controls.control-text{id: "attachment_#{attachment.id}"} = f.hidden_field :attachments, value: attachment.signed_id, multiple: true - = link_to url_for(attachment), target: "_blank" do + = link_to attachment.url, target: "_blank" do = image_tag attachment.preview(resize_to_limit: [100, 100]) if attachment.previewable? = image_tag attachment.variant(resize_to_limit: [100, 100]) if attachment.variable? = link_to 'Delete', delete_attachment_finance_invoice_path(f.object, attachment_id: attachment.id), method: :delete, remote: true, class: 'btn' diff --git a/app/views/admin/users/show.html.haml b/app/views/admin/users/show.html.haml index dcf9ba360..cc4e3076c 100644 --- a/app/views/admin/users/show.html.haml +++ b/app/views/admin/users/show.html.haml @@ -22,7 +22,7 @@ %dt= heading_helper User, :attachment - for attachment in @user.attachments %dd - =link_to url_for(attachment), target: "_blank" do + =link_to attachment.url, target: "_blank" do = image_tag attachment.preview(resize_to_limit: [100, 100]), style: 'margin: 0px 10px 10px 0px;' if attachment.previewable? = image_tag attachment.variant(resize_to_limit: [100, 100]), style: 'margin: 0px 10px 10px 0px;' if attachment.variable? = attachment.filename diff --git a/app/views/finance/invoices/_form.html.haml b/app/views/finance/invoices/_form.html.haml index cd9faf3f1..8428bfd8e 100644 --- a/app/views/finance/invoices/_form.html.haml +++ b/app/views/finance/invoices/_form.html.haml @@ -31,7 +31,7 @@ - f.object.attachments.reject(&:new_record?).each do |attachment| .controls.control-text{id: "attachment_#{attachment.id}"} = f.hidden_field :attachments, value: attachment.signed_id, multiple: true - = link_to url_for(attachment), target: "_blank" do + = link_to attachment.url, target: "_blank" do = image_tag attachment.preview(resize_to_limit: [100, 100]) if attachment.previewable? = image_tag attachment.variant(resize_to_limit: [100, 100]) if attachment.variable? = link_to 'Delete', delete_attachment_finance_invoice_path(f.object, attachment_id: attachment.id), method: :delete, remote: true, class: 'btn' diff --git a/app/views/finance/invoices/show.html.haml b/app/views/finance/invoices/show.html.haml index 3cd96b5f2..4d80ea956 100644 --- a/app/views/finance/invoices/show.html.haml +++ b/app/views/finance/invoices/show.html.haml @@ -67,7 +67,7 @@ %dt= heading_helper(Invoice, :attachment) + ':' - for attachment in @invoice.attachments %dd - =link_to url_for(attachment), target: "_blank" do + =link_to attachment.url, target: "_blank" do = image_tag attachment.preview(resize_to_limit: [100, 100]), style: 'margin: 0px 10px 10px 0px;' if attachment.previewable? = image_tag attachment.variant(resize_to_limit: [100, 100]), style: 'margin: 0px 10px 10px 0px;' if attachment.variable? = attachment.filename diff --git a/app/views/finance/invoices/unpaid.html.haml b/app/views/finance/invoices/unpaid.html.haml index cf78ff263..e04d514e0 100644 --- a/app/views/finance/invoices/unpaid.html.haml +++ b/app/views/finance/invoices/unpaid.html.haml @@ -27,7 +27,7 @@ = number_to_currency(invoice_amount_diff.abs) - if invoice.attachments.attached? - for attachment in invoice.attachments - = link_to attachment.filename, url_for(attachment) + = link_to attachment.filename, attachment.url = glyph :download - if invoice.note? = '(' + invoice.note + ')'