如何指定 ActiveAdmin 文件选择器的格式?
How can I specify formats for ActiveAdmin file picker?
我有我的 ActiveAdmin 表单:
form do |f|
f.inputs 'Details' do
f.input :orders_file, as: :file
end
actions
end
而且我想指定文件选择器仅搜索 .xls、.xlsx 文件,但我似乎找不到具体方法。
此外,如果未选择文件,我怎样才能使文件成为必需文件而不影响控制器?
非常感谢!
您可以使用 HTML
的接受属性
f.input :orders_file, as: :file, required: true, input_html: {accept: ".xls, .xlsx"}
您还可以在控制器操作中检查文件扩展名是否为 xls
if !params[:file].path.split('.').last.to_s.casecmp?('xls')
flash[:error] = 'Please upload valid xls file'
redirect_back fallback_location: { action: 'index' }
else
# write your code
end
我有我的 ActiveAdmin 表单:
form do |f|
f.inputs 'Details' do
f.input :orders_file, as: :file
end
actions
end
而且我想指定文件选择器仅搜索 .xls、.xlsx 文件,但我似乎找不到具体方法。
此外,如果未选择文件,我怎样才能使文件成为必需文件而不影响控制器?
非常感谢!
您可以使用 HTML
的接受属性f.input :orders_file, as: :file, required: true, input_html: {accept: ".xls, .xlsx"}
您还可以在控制器操作中检查文件扩展名是否为 xls
if !params[:file].path.split('.').last.to_s.casecmp?('xls')
flash[:error] = 'Please upload valid xls file'
redirect_back fallback_location: { action: 'index' }
else
# write your code
end