0

无法弄清楚如何做到这一点?并且在其他任何地方都找不到太多帮助!

我已经像这样设置了回形针和雾;

配置/初始化程序/fog.rb

     connection = Fog::Storage.new({
       :provider           => 'Rackspace',
       :rackspace_username => '',
       :rackspace_api_key  => ''
     })

环境.rb;

    Paperclip::Attachment.default_options.update({
    :path             => ":attachment/:id/:timestamp_:style.:extension",
    :storage          => :fog,
    :fog_credentials  => {
      :provider           => 'Rackspace',
      :rackspace_username => '',
      :rackspace_api_key  => '',
      :persistent         => false
    },
    :fog_directory    => '',
    :fog_public       => true

})

file_field用来获取图像,然后将其发布到我的控制器。这让我得到了这样的东西;

"pic"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007f90ac06a6c8 @original_filename="3245-1920x1200.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"cloth[pic][image]\"; filename=\"3245-1920x1200.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20130104-5386-103laem>>}

我无法理解的是,我该如何使用类似的方法将此文件实际保存到云文件中;

file = directory.files.create(
  :key    => 'resume.html',
  :body   => File.open("/path/to/my/resume.html"),
  :public => true
)

编辑

相关模型;

class Cloth
  include Mongoid::Document
  has_many :pics

class Pic
  include Mongoid::Document
  include Mongoid::Paperclip
    belongs_to :cloth

    has_mongoid_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

在控制器中,这就是我当前基于上述参数创建图片的方式;

@cloth = Cloth.new
@cloth.pics.create!(params[:cloth][:pic])
4

2 回答 2

3

让我们退后一步,从不同的角度来看问题。您能否查看以下脚本是否会将图像上传到您的容器:

require 'fog'
service = Fog::Storage.new({
  :provider                 => 'Rackspace',
  :rackspace_username        => YOUR_USERNAME,
  :rackspace_api_key    => YOUR_API_KEY
  })

container = service.directories.new(:key => YOUR_CONTAINER_NAME)
container.files.create :key => 'my-pix.jpg', :body => File.open PATH_TO_FILE

用适当的变量更新大写参数,让我知道会发生什么。希望这将有助于缩小问题的范围。

于 2013-01-07T21:16:04.533 回答
2

Paperclip 和 ActiveRecord 应该会自动为您处理文件上传。这是一个很好的快速入门解释该过程:

https://github.com/thoughtbot/paperclip#quick-start

如果还是有问题,能否提供相关的控制器和型号代码给我?

于 2013-01-04T16:34:46.617 回答