使用在Windows Azure VM上运行的Paperclip的Rails应用程序

时间:2013-03-28 16:27:51

标签: ruby-on-rails azure imagemagick paperclip azure-storage

我在Windows Azure虚拟机上运行Rails(3.2.9)应用程序(使用Ruby-Stack-1-9-3-6-Ubuntu-12-10映像)。我可以使用Capistrano成功部署到VM,但在文件上传和Azure存储方面已经到了墙。

我正在尝试实现使用paperclip-azure-storage gem的waz-storage gem,但似乎无法通过此错误:

undefined method 'new' for nil:NilClass

每当我尝试创建/更新/销毁Video对象(使用简单的Rails表单)时,我都会收到此错误。为了澄清,我在本地计算机和VM实例上都收到此错误。

我知道错误在于Paperclip,因为只要我在视频模型中注释掉以下代码,一切正常:

has_attached_file :pic, :storage => :azure1

按照两个宝石来源(上面的链接)的说明,我添加了以下文件:

  • azure.yml(包含我的Azure存储帐户名和访问密钥)
  • storage.rb(从paperclip-azure-storage gem复制并添加到/ lib / paperclip)
  • paperclip.rb(也从paperclip-azure-storage gem复制并添加到初始化程序列表中)

涵盖基础知识:

  • Gemfile:gem 'paperclip'刚刚更新到版本3.4.1
  • 在表单中<%= form_for @video, html: { multipart: true } do |video_form| %>尝试使用和不使用url选项。
  • 在每个环境文件中:Paperclip.options[:command_path] = "/opt/local/bin/convert"
  • 架构:已经过验证,以确保pic附件已正确添加到视频表

我已经不知疲倦地研究了答案,但我似乎无法找到问题的根源。是不是ImageMagick没有正确安装?是storage.rbpaperclip.rb文件的展示位置和/或配置是否错误?或者它完全是另一回事?

以下是我的paperclip.rb初始化程序中的代码:

module Paperclip
  class Attachment
    def self.default_options
      @default_options ||= {
        :styles            => {},
        :processors        => [:thumbnail],
        :convert_options   => {},
        :default_url       => "/rails.png",
        :default_style     => :original,
        :whiny             => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
        :storage           => :azure1,
        :path              => ":modelname/:attachment/:id/:style/:filename",        
        :azure_credentials => "#{Rails.root}/config/azure.yml",
        :azure_container   => "system",
        :azure_host_alias  => "name_of_azure_storage.blob.core.windows.net",
        :url               => ':azure_domain_url',
      }
    end    
  end
end

如果需要,我很乐意发布其他代码。

有没有人尝试/成功使用Windows Azure实现上述宝石?我很乐意接受任何指导/建议。

1 个答案:

答案 0 :(得分:0)

问题似乎是当你使用格式时:

module Paperclip
  class attachment
    ...
  end
end

不会创建Paperclip的内部默认选项。然后它尝试实例化URL生成器,该生成器通常在那些内部默认选项中定义,这会导致您看到的错误。

为了解决这个问题,我将初始化程序格式化如下:

Paperclip::Attachment.default_options[:processors] = [:thumbnail]
Paperclip::Attachment.default_options[:convert_options] = {}
Paperclip::Attachment.default_options[:styles] = {}
Paperclip::Attachment.default_options[:default_url] = "/rails.png"
Paperclip::Attachment.default_options[:default_style] = :original
Paperclip::Attachment.default_options[:whiny] = Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
Paperclip::Attachment.default_options[:storage] = :azure1
Paperclip::Attachment.default_options[:path] = ":modelname/:attachment/:id/:style/:filename"
Paperclip::Attachment.default_options[:azure_credentials] = "#{Rails.root}/config/azure.yml"
Paperclip::Attachment.default_options[:azure_container] = "system"
Paperclip::Attachment.default_options[:azure_host_alias] = "storageaccount.blob.core.windows.net"
Paperclip::Attachment.default_options[:url] = ':azure_domain_url'

这允许使用我的初始化程序中的选项成功创建/合并默认选项,并让我超越该错误。

由于我使用的是最新版本的rails,我接下来遇到RAILS_ROOT和RAILS_ENV错误,因此我调整了上面的初始化位以使用Rails.root并更改了paperclip-azure-storage以使用Rails.env代替RAILS_ENV。

希望这有帮助。