4

我正在尝试为我的 rails 3.2 应用程序中的某些图像上传设置carrierwave_direct,但似乎无法使用direct_upload_form_for 标签呈现表单。

加载包含表单的页面时,我收到错误“#< PostmarkerImage:0x007fdbe07b39f0 > 的未定义方法`direct_fog_url'”。

PostmarkerImage 是我安装上传器的模型,如下所示:

class PostmarkerImage < ActiveRecord::Base
  attr_accessible :image, :image_cache

  belongs_to :postmarker

  validates :image, :presence => true

  mount_uploader :image, PostmarkerImageUploader
end

图像列是一个字符串,我还确保已运行迁移以创建该列。上传器现在看起来像这样:

class PostmarkerImageUploader < CarrierWave::Uploader::Base
  include CarrierWaveDirect::Uploader

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  # Choose what kind of storage to use for this uploader:
  # storage :file
  # storage :fog

  include CarrierWave::MimeTypes
  process :set_content_type

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :scale => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

我还确保已经安装了carrierwave_direct、fog、mini_magick 和Imagemagick。我可以知道我还错过了什么吗?似乎上传器的安装没有正确完成。

4

2 回答 2

5

设法弄清楚:)以防万一有人遇到这个问题:

我正在实现一个单独的模型来保存我的图像,而不是在现有表中添加一列。所以direct_upload_form_for 期望(例如Image.new.image)而不是我返回的对象(例如Image.new)。我想我被 form_for 部分抛弃了,它通常需要一个Object

于 2013-01-04T04:07:07.813 回答
1

对于遇到此错误的任何其他人,您可能没有将正确的对象传递给 direct_upload_form_for。假设我们有一个带有头像列的用户模型。它应该看起来像。

模型:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

控制器:

def edit # Could be any action, but I always direct upload to existing objects
  @uploader = @user.avatar
  @uploader.success_action_redirect = edit_registration_url(resource)
end

查看(erb):

<%= direct_upload_form_for @uploader do |f| %>
  <%= f.file_field :image %>
<% end %>

注意@uploader 对象实际上是@user.avatar列,而不是@user对象。这就是OP在他的回答中所说的。在他的情况下更令人困惑,因为该对象不是用户,而是 PostmarkerImage,我认为它是一个仅用于保存图像的模型。当您希望一个对象(即邮戳)有多个图像时,这很常见。因此,您仅为图像创建模型,并has_many在它们之间建立关系。

陷阱:我在生产中也经常遇到这个错误,因为我忘记将控制器中的代码从edit动作复制到update动作。如果由于验证(如文件类型)导致上传失败,您需要记住再次设置@uploader!同样,如果您的上传者位于 中new,请记住也将 @uploader 分配到 中create

于 2014-07-11T04:27:57.150 回答