CarrierWave 与 ActiveResource

发布于 2024-10-21 08:27:36 字数 213 浏览 5 评论 0原文

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

我尝试了一些事情,但没有取得太大成功(或者确信我在远程正确地做了任何事情),所以我很感激任何成功实现 CarrierWave 而不使用 gem 中已经包含的 ORM 模块的人的建议。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2024-10-28 08:27:36

我可能迟到了,因为原作者已经继续前进,但是当有人搜索“载波活动资源”时,这个问题出现在顶部,所以我认为它仍然值得回答。

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

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

当您保存艺术家时:

artist.save!

图片也会被保存。

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

class Artist < ActiveResource::Base
end

如果我随后读到一个 Artist:

artist = Artist.find(1)

并查看它,我会在那里找到这个:

#<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 来抓取图片。但我们来谈谈上传另一张图片。

我们可以将这一点代码添加到服务器端的 Artist 模型中:

  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

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

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

此时,在资源方面,您只需将“artist_picture”设置为文件名,它就会在保存资源时进行编码和发送。在服务器端,文件将被解码并保存。想必您可以通过强制字符串进行二进制编码来跳过 Base64 编码,但当我这样做时,它会很糟糕,而且我没有耐心去追踪它。编码为 base64 有效。

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.

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')

And when you save artist:

artist.save!

the picture will be saved, also.

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>

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文