带有ActiveResource的CarrierWave [英] CarrierWave with ActiveResource

查看:42
本文介绍了带有ActiveResource的CarrierWave的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人对将CarrierWave与ActiveResource模型结合使用有任何见解(在Rails 3中)?我有一个ActiveResource模型,其中包含用于文件名的字段,我想将文件保存到远程文件系统中。

Does anyone have any insights into using CarrierWave with an ActiveResource model (in Rails 3)? I've got an ActiveResource model with field for the filename, and I want to save the file to the remote filesystem.

我尝试了几件事但没有成功(或者确信我在远程正确地执行了任何操作),因此,我很感谢成功实施CarrierWave而不使用gem中已经包含的ORM模块的任何人的建议。

I've tried a few things without much success (or conviction that I was doing anything remotely correctly), so I'd appreciate suggestions from anyone who's successfully implemented CarrierWave without using the ORM modules already included in the gem.

推荐答案

由于原作者继续前进,我可能已经很晚了,但是当有人问到这个问题时

I'm probably late for this as the original author has moved on, but this question comes up at the top when someone searches for "carrierwave activeresource", so I thought it was still worth answering.

为了便于讨论,我们假设我们有一个名为Artist的模型,其图片名为artist_picture安装为CarrierWave上传器。使用ActiveRecord,您可以将此图片分配给文件:

For the sake of discussion, let's assume we have a model named Artist with a picture named artist_picture mounted as a CarrierWave uploader. With ActiveRecord, you would assign this picture to a File:

artist.artist_picture=File.open('ravello.jpg')

保存艺术家时:

artist.save!

图片也将被保存。

现在,假设我基于此创建资源:

Now, let's say I create a resource based on this:

class Artist < ActiveResource::Base
end

如果随后我在艺术家中阅读:

If I subsequently read in an artist:

artist = Artist.find(1)

并查看它,我会在其中找到它:

and look at it, I'll find this in there:

#<Artist:0x39432039 @attributes={"id"=>1, "name"=>"Ravello", "artist_picture"=>#<ArtistPicture:0x282347249243 @attributes={"url"=>"/uploads/artists/artist_picture/1/ravello.jpg"}, @prefix_options={}, @persisted=false>, @prefix_options={}, @persisted=false>

有趣的是,artist_picture本身就是一个模型,我们可以声明它并在需要时使用它。实际上,如果需要,您可以使用url来获取图片。但是让我们谈谈上传另一张图片。

Interestingly, artist_picture is itself a model and we could declare it and play around with it if we wanted. As it is, you can use the url to grab the picture if you want. But let's talk instead about uploading another picture.

我们可以在服务器端的Artist模型中添加以下代码:

We can add this little bit of code to the Artist model on the server side:

  def artist_picture_as_base64=(picsource)
    tmpfile = Tempfile.new(['artist','.jpg'], Rails.root.join('tmp'), :encoding => 'BINARY')
    begin
      tmpfile.write(Base64.decode64(picsource.force_encoding("BINARY")))
      file = CarrierWave::SanitizedFile.new(tmpfile)
      file.content_type = 'image/jpg'
      self.artist_picture = file
    ensure
      tmpfile.close!
    end
  end

我只是显示一个简单的示例-您应该也可能会传递原始文件名。无论如何,在资源方面:

I'm just showing a simple example - you should probably pass the original filename, also. Anyway, on the resource side:

class Artist < ActiveResource::Base
  def artist_picture=(filename)
    self.artist_picture_as_base64=Base64.encode64(File.read(filename))
  end
end

这时,在资源端,您只需将 artist_picture设置为文件名,它将在资源时被编码并发送已保存。在服务器端,文件将被解码并保存。大概您可以通过仅强制将字符串转换为二进制编码来跳过base64编码,但是这样做的话它会崩溃并且我没有耐心来追踪它。编码为base64即可。

At this point, on the resource side you need only set "artist_picture" to a filename and it will be encoded and sent when the resource is saved. On the server side, the file will be decoded and saved. Presumably you could skip base64 encoding by just forcing the string to binary encoding, but it craps when I do that and I don't have the patience to track it down. Encoding as base64 works.

这篇关于带有ActiveResource的CarrierWave的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆