gpt4 book ai didi

jquery - 载波;在生产服务器上保存图像并移动它们?

转载 作者:数据小太阳 更新时间:2023-10-29 08:08:07 27 4
gpt4 key购买 nike

编辑:我的文件仅按照缓存而非 store_dir 的规定存储

我花了几个小时试图让它工作,Carrierwave 在本地工作,但在我的生产中,它们只保存在 Cache_dir 中,并且表单没有将我重定向到我指定的路径。

是否需要将文件从已发布的 TMP 文件夹移动到它应该在的位置?或者 carrierwave 不应该自动执行此操作吗?

如果有人想提供帮助,请发布大量代码。

我的文件被保存@:

/releases/20150601122454/public/uploads/tmp/pics/1433164111-29887-8666/elg.jpg 

我正在尝试用以下方式移动它:

require 'fileutils'
tmp = params[:instruction][:image].tempfile
newfile = File.join("public/system/pics",params[:instruction][:image].original_filename)
FileUtils.touch('newfile')
FileUtils.copy_file(tmp.path,newfile)

这只会崩溃说

No such file or directory - public/system/pics/elg.jpg

她是我的 Image_Uploader:

class ImageUploader < CarrierWave::Uploader::Base

# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog




# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"system/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

def cache_dir
"uploads/tmp/pics"
end

这是我更新模型的方式:

def update
file = params[:instruction][:image]
puts @instruction.text + " HERE IT IS " +file.path
@instruction.image = file
@instruction.save
Rails.logger.info("@inst.image path: " + @instruction.image.path)

require 'fileutils'
# tmp = params[:instruction][:image].tempfile
# newfile = File.join("public/system/pics",params[:instruction][:image].original_filename)
#FileUtils.touch('newfile')
#FileUtils.copy_file(tmp.path,newfile)

if @instruction.update(instruction_params)
redirect_to instructions_path, notice: 'Instruktionerna sparades.'
else
render action: 'edit' , alert: 'Misslyckades att uppdatera instruktionerna'
end

结束

 def instruction_params
params.require(:instruction).permit(:text,:role_id)
end

我的表格:

<%= form_for @instruction, :html => {:multipart => true} do |f| %>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
<br>
<div>
<%= f.file_field :image%>

<%= f.hidden_field :image_cache %>
</div>
<br>
<div class="form-group">
<%= f.text_area :text, rows: 15, class: "form-control" %>
</div>
</div>
</div>

<%= f.submit 'Spara', class: "btn btn-primary" %>
<% end %>

模型:

class Instruction < ActiveRecord::Base
belongs_to :role
mount_uploader :image, ImageUploader
end

拜托拜托,我真的需要帮助。

最佳答案

可能的方法 #1 - 添加一个 /#{Rails.root}/public

File.join("/#{Rails.root}/public/system/pics",params[:instruction][:image].original_filename)

可能的方法 #2

Verify that Rails has permission to read/write to your file system.

引用类似于我在生产atm中运行的代码

这与我们在生产中使用的代码相似,效果很好。

class SomeUploader < CarrierWave::Uploader::Base
...

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
if Rails.env.production?
"uploads/prod/#{model.id}"
else if Rails.env.staging?
"uploads/staging/#{model.id}"
else
"uploads/dev/#{model.id}"
end
end

关于jquery - 载波;在生产服务器上保存图像并移动它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30574687/

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