In my Rails 5.2 app I have this controller which sends a download link with a token
parameter to a user:
class ArchivesController < ApplicationController
def new
# Create and email download link
end
def show
archive = Archive.valid.find_by(:token => params[:id]) # Check if archive is still valid or has already expired
if archive
redirect_to rails_blob_path(archive.file)
else
flash[:notice] = "Invalid link!"
redirect_to root_path
end
end
end
This works and correctly redirects to the rails_blob_path
which is provided by Rails' ActiveStorage.
What bothers me is that clicking the download link will not only download the file but also open a new (blank!) browser tab. I guess this is due to the redirect in my controller action?
Is there any way to prevent browsers from opening a blank browser tab? In my eyes this is not required and may confuse the user.
In a previous version of my app I put the rails_blob_path
directly in the email and the download was triggered without opening a new browser tab. However, I didn't like that approach for security reasons and there was no way to invalidate or expire records that had already been downloaded.