我想将图像直接从This上传到资产文件夹,因此我创建了一个控制器和一个带有表单的自定义页面。我在tmp文件夹中检索上载器文件,并将其复制到public/assets/images
文件夹中,然后将URL存储在ActiveAdmin设置中,并在主页上显示图像。但是我不知道为什么,但是在开发中我没有找到错误404。
GET http://localhost:3000/public/assets/images/RackMultipart20180724-9774-1qgvmge.png 404 (Not Found)
我还没有在生产中尝试过,但是它是在debian挤压下运行的专用服务器,您认为它可以工作吗?
class Administration::SettingsController < ApplicationController
FILE_PATH = "#{Rails.root}/public/assets/images/"
def update
errors = []
complete = 0
setting_params.each do |setting|
setting = Setting.find_by(var: setting[0]) || Setting.new(var: setting[0])
if setting.value != setting_params[setting.var]
setting.value = setting_params[setting.var]
if setting.save
complete += 1
else
errors << {setting: setting, error: setting.errors.full_messages}
end
end
end
redirect_to admin_settings_path, notice: 'Setting has updated.' if errors.count == 0
redirect_to admin_settings_path, alert: 'Setting update error.' if errors.count > 0
end
def upload_file
create_directory
tempfile = setting_params[:homepage_image].tempfile
file_name = File.basename(tempfile.path)
FileUtils.mv(tempfile, FILE_PATH)
new_file_path = FILE_PATH + file_name
if File.exist?(new_file_path)
Setting.homepage_image = '/public/assets/images/' + file_name
redirect_to admin_settings_path, notice: 'Setting has updated.'
else
redirect_to admin_settings_path, alert: 'Setting update error.'
end
end
private
def setting_params
params[:settings][:show_number_of_cars_in_results] = false if params[:settings][:show_number_of_cars_in_results].nil?
params.require(:settings).permit(:show_number_of_cars_in_results, :homepage_image)
end
def create_directory
unless File.directory?(FILE_PATH)
FileUtils.mkdir_p FILE_PATH
end
end
end