Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Uploader会产生关于它应该在其中存储路径的列的错误_Ruby On Rails_Ruby_File Upload_Amazon S3_Carrierwave - Fatal编程技术网

Ruby on rails Uploader会产生关于它应该在其中存储路径的列的错误

Ruby on rails Uploader会产生关于它应该在其中存储路径的列的错误,ruby-on-rails,ruby,file-upload,amazon-s3,carrierwave,Ruby On Rails,Ruby,File Upload,Amazon S3,Carrierwave,图像模型与组织模型具有1:1的关联。在组织控制器中,create方法调用名为upload\u file的图像模型方法 def create @organization = Organization.new(new_params) if @organization.save Image.upload_file(@organization.id) end end upload\u file方法使用carrierwave上传程序将标准文件存储在AmazonS3存储桶中。为此,图像

图像模型与组织模型具有1:1的关联。在组织控制器中,
create
方法调用名为
upload\u file
的图像模型方法

def create
  @organization = Organization.new(new_params)
  if @organization.save
    Image.upload_file(@organization.id)
  end
end
upload\u file
方法使用carrierwave上传程序将标准文件存储在AmazonS3存储桶中。为此,图像模型包括
mount\u uploader:file\u name,ImageUploader

我的问题是如何为上传的文件创建图像实例?存储文件的路径应存储在图像模型的
file\u name
列中。与图像关联的组织应存储在图像模型的
organization\u id
列中。我该怎么做?更具体地说,我应该为下面的模型方法添加什么代码?(另请参见以下方法中的注释

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
当前在
rails控制台中
I出现以下错误:

>> 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.
uploader=ImageUploader.new => # >>文件='app/assets/emptyfile.xml' =>“app/assets/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'cache!' 等
您不必亲自调用上传程序。Carrierwave提供了一种机制,可以在AR型号中为您进行上传和存储:

class Organization < ActiveRecord::Base
  mount_uploader :image, ImageUploader
end

请访问carrierwave以获得更广泛的示例。

谢谢Axel,我想我在文档中看到了一个错误的部分,因为
store!
也是carrierwave方法,但在另一个部分中。我在控制台中尝试了以下操作:
uploader=ImageUploader.new
文件.open('app/assets/emptyfile.xml'))执行| f |
uploader.file_name=f
结束
uploader.save!
。但是,这会产生错误:
NoMethodError:for#
的未定义方法“file_name=”。此外,这还不会为它创建的映像实例保存组织id。请不要直接使用上载程序。这对于您来说是错误的案例。如上图所示装载上载程序时,您将获得如上所示的“image=”方法。您的组织本身将有一个映像来访问它。因此,您不必将组织id“存储在映像中”。您仍在自己创建ImageUploader的新实例-这不是您应该结合使用上载程序的方式使用AR模型使用上面示例中的代码(注意没有ImageUploader.new…)啊,我看到了,正在取得进展(更新了帖子中的更新)。我有一个验证,在实现上传器之前,它正在工作:
errors.add(:file\u name,“file format error”),除非(file\u name.match(/\.xml\z/I))
。此验证现在会产生一个错误:
NoMethodError(未定义的方法“match”for#)
。它是否会将上载的文件视为字符串而不是文件?
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'