这是图像模式有一个1:1的关联与组织模式。在组织控制器,创建
方法在一个名为 upload_file
。
高清创建
@organization = Organization.new(new_params)
如果@ organization.save
Image.upload_file(@ organization.id)
结束
结束
在 upload_file
方法使用carrierwave上传来存储标准文件在Amazon S3的桶中。为此,图像模型包括 mount_uploader:FILE_NAME,ImageUploader
我的问题是如何为上传的文件创建一个Image实例?的路径保存的文件应存放在 FILE_NAME
在图像模型中的列。而与图像相关的组织应存放在 ORGANIZATION_ID
在图像模型中的列。我怎样才能做到这一点?更具体地说,什么code,我应该为它添加到下面的模型法? (另见注释在下面的方法
高清self.upload_file(ORGANIZATION_ID)
文件='应用程序/资产/ emptyfile.xml
上传= ImageUploader.new
uploader.store!(文件)
#我是正确的假设,previous线上传使用上传的文件,但还没有建立一个影像纪录?
#如果是这样的话,或许下一行应该如下?:
#Image.create!(ORGANIZATION_ID:ORGANIZATION_ID,文件名:file.public_url)
#我提出了file.public_url。什么是正确的code到包括上载存储图像的(在我的情况下,亚马逊S3存储)的路径?
结束
目前在轨控制台
我收到以下错误:
>>上传= ImageUploader.new
=> #< ImageUploader:0x00000005d24f88 @模型=零,@ mounted_as =零,@ fog_directory =零>
>>文件='应用程序/资产/ emptyfile.xml
=> 应用程序/资产/ emptyfile.xml
>> uploader.store!(文件)
CarrierWave :: FormNotMultipart:CarrierWave :: FormNotMultipart
从/usr/local/rvm/gems/ruby-2.2.1/gems/carrierwave-0.10.0/lib/carrierwave/uploader/cache.rb:120:in`缓存!
等等
解决方案
您不必调用上传者自己。 Carrierwave配备了一个机制,做AR模型上传和存储您:
类组织与LT;的ActiveRecord :: Base的
mount_uploader:图像,ImageUploader
结束
然后,你可以做
U = Organization.new
u.image = PARAMS [:文件]#将这样的文件,或者
# 喜欢这个
File.open方法('地方')办| F |
u.image = F
结束
u.save!
u.image.url#=> /url/to/file.png
u.image.current_path#=> 路径/要/ file.png
u.image#=> file.png
请访问carrierwave 自述获得更广泛的例子。
An Image model has a 1:1 association with an Organization model. In the organizations controller, the create
method calls on an Image model method called upload_file
.
def create
@organization = Organization.new(new_params)
if @organization.save
Image.upload_file(@organization.id)
end
end
The upload_file
method uses a carrierwave uploader to store a standard file in an Amazon S3 bucket. To this end, the Image model includes mount_uploader :file_name, ImageUploader
.
My question is how to create an Image instance for the uploaded file? The path to the stored file should be stored in the column file_name
in the Image model. And the organization associated with the image should be stored in the column organization_id
in the Image model. How can I do this? More specifically, what code should I add for this to the model method below? (also see the comments in the method below.
def self.upload_file(organization_id)
file = 'app/assets/emptyfile.xml'
uploader = ImageUploader.new
uploader.store!(file)
# Am I correct to assume that the previous line uploads the file using the uploader, but does not yet create an Image record?
# If so, then perhaps the next line should be as follows?:
# Image.create!(organization_id: organization_id, filename: file.public_url)
# I made up "file.public_url". What would be the correct code to include the path that the uploader stored the image at (in my case an Amazon S3 bucket)?
end
Currently in rails console
I get the following error:
>> uploader = ImageUploader.new
=> #<ImageUploader:0x00000005d24f88 @model=nil, @mounted_as=nil, @fog_directory=nil>
>> file = 'app/assets/emptyfile.xml'
=> "app/assets/emptyfile.xml"
>> uploader.store!(file)
CarrierWave::FormNotMultipart: CarrierWave::FormNotMultipart
from /usr/local/rvm/gems/ruby-2.2.1/gems/carrierwave-0.10.0/lib/carrierwave/uploader/cache.rb:120:in `cache!'
etc.
解决方案
You don't have to call the uploader yourself. Carrierwave comes with a mechanism to do the uploading and storing in AR models for you:
class Organization < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
Then you can do
u = Organization.new
u.image = params[:file] # Assign a file like this, or
# like this
File.open('somewhere') do |f|
u.image = f
end
u.save!
u.image.url # => '/url/to/file.png'
u.image.current_path # => 'path/to/file.png'
u.image # => 'file.png'
Please visit the carrierwave README for more extensive examples.