编辑:我的文件只按照缓存而非store_dir
的方式存储我花了好几个小时试图让它工作,Carrierwave在本地工作,但在我的制作中,它们只保存在Cache_dir中,表单不会将我重定向到我指定的路径。
是否需要将文件从其发布的TMP文件夹移动到应该的位置?或者不应该自动执行此操作?
如果有人想要帮助,则发布大量代码。
我的文件被保存@:
/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)
这只是崩溃说
没有这样的文件或目录 - 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
请,我真的需要帮助。
答案 0 :(得分:1)
可能的方法#1 - 添加/#{Rails.root}/public
File.join("/#{Rails.root}/public/system/pics",params[:instruction][:image].original_filename)
可能的方法#2
验证Rails是否具有读/写文件系统的权限。
类似于我在生产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
答案 1 :(得分:0)
尝试覆盖store_dir:
以下代码段属于运营商文档:
要更改上传文件的放置位置,只需覆盖store_dir方法:
# Configure carrierwave root directory
# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.root = Rails.root
end
# Overwrite store dir
class MyUploader < CarrierWave::Uploader::Base
def store_dir
'public/my/upload/directory'
end
end
这适用于文件存储以及Amazon S3和Rackspace云文件。如果您想在根级别存储文件,请将store_dir定义为nil。
如果将文件存储在项目根文件夹之外,则可能需要以相同的方式定义cache_dir:
class MyUploader < CarrierWave::Uploader::Base
def cache_dir
'/tmp/projectname-cache'
end
end
答案 2 :(得分:0)
我通过这个肮脏的把戏解决了它:
require 'fileutils'
tmp = @instruction.image
newfile = File.join("/#{Rails.root}/public/system/pics",params[:instruction][:image].original_filename)
if File.exists?(newfile)
else
FileUtils.makedirs("/#{Rails.root}/public/system/pics")
end
FileUtils.mv(tmp.path,newfile)
@instruction.image = File.open(newfile)
@instruction.save
答案 3 :(得分:0)
今天花了很多时间来解决同样的问题:CarrierWave只保存服务器上的tmp文件(带原始名称的uploads / tmp文件夹)并且不保存记录到DB(只是BEGIN ROLLBACK)。 解决方案是如此基本:在我的服务器上安装imagemagick - 我忘了它:)