我知道 ActiveStorageurl_for(user.avatar)
方法。例子:
<%= link_to "Download this file", url_for(@user.avatar) %>
这很棒,但似乎没有围绕它建立的授权。知道此链接的任何人都可以下载此文档。
过去当我使用回形针时,我有一个指向自定义控制器操作的链接。该自定义控制器操作进行了授权,如果一切正常,那么我曾经send_file
将文件发送给用户。是这样的:
def deliver_the_file
authorize :my_authorization
send_file @user.avatar.path
end
如何使用 active_storage 做到这一点?我所做的只是url_for
完全不授权用户的实现。
我正在专门查看有关 ActiveStorage 的 Rails 指南的下载文件部分。
相关示例代码:
# model
class Book < ApplicationRecord
has_one_attached :book_cover
end
class BooksController < ApplicationController
...
# custom action to authorize and then deliver the active_storage file
def deliver_it
# ... assume authorization already happened
send_file rails_blob_path(@book.book_cover, disposition: "attachment")
end
end
这错误并说:
无法读取文件
我也尝试过这个:
def deliver_it
# ... assume authorization already happened
send_file @book.book_cover.download
end
这返回了错误:
字符串包含空字节
我也尝试过这个:
def deliver_it
@book.book_cover.download
end
这返回了错误:
BooksController#deliver_it 缺少此请求的模板
我也试过这个:
def deliver_it
send_file @book.book_cover.blob
end
这错误并说:
没有将 ActiveStorage::Blob 隐式转换为 String