gpt4 book ai didi

ruby-on-rails-3 - ActiveAdmin 中的文件下载链接

转载 作者:行者123 更新时间:2023-12-04 02:38:14 25 4
gpt4 key购买 nike

如何在 ActiveAdmin 中创建文件下载链接以下载上传的文件?

我试过这样的:

action_item :only=> :show do
link_to('Download file', [@user, :upload_file])
end

最佳答案

action_item 将在任务栏中创建按钮,但它不会创建实际的路由或 Controller 的操作方法。

您可以通过在 user.rb 中创建自定义 member_action 来实现下载功能

# In app/admin/users.rb
action_item only: [:show] do
link_to('Download File', download_admin_user_path(resource)) if resource.upload_file.present?
end

member_action :download, method: :get do
user = User.find(params[:id])
send_file user.upload_file
end

或者,我的偏好是利用 upload_file.rb

中的 RESTful 显示操作
# In app/admin/users.rb
action_item only: [:show] do
# NOTE: remove period from file extension
file_ext = resource.upload_file.file_extension.gsub('.', '')
link_to('Download File', download_admin_upload_file_path(resource.upload_file, format: file_ext)) if resource.upload_file.present?
end

# In app/admin/upload_files.rb
controller do
def show
if params[:format] == 'txt' # example of a known mimetype, e.g. text file
send_data resource.path, type: 'text/plain'
elsif params[:format] == resource.file_extension # example of unknown mimetype
send_file resource.path
# let ActiveAdmin perform default behavior
else
super
end
end
end

引用:http://activeadmin.info/docs/8-custom-actions.html

关于ruby-on-rails-3 - ActiveAdmin 中的文件下载链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14353554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com