CarrierWave:不保存图像(仅限tmp)

时间:2012-03-18 17:34:55

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 carrierwave

我有CarrierWave的问题。数据库中的图像和路径未保存,但tmp文件为。

这是我的AvatarUploader(apps / uploaders / avatar_uploader.rb):

# encoding: utf-8

class AvatarUploader < CarrierWave::Uploader::Base

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

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

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
#   "/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

这是会员模型(apps / models / member.rb):

class Member < ActiveRecord::Base

    attr_accessible :name, :email, :password, :password_confirmation, :avatar

    mount_uploader :avatar, AvatarUploader

    email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

    validates :name, presence: true,
                                     uniqueness: true,
                                     length: { within: 4..32 }
    validates :email, presence: true,
                                        uniqueness: true,
                                        format: { with: email_regex }
    validates :password, presence: true,
                                             length: { minimum: 6 }

    has_secure_password

    before_create :generate_token

    private

        def generate_token
            begin
                self.auth_token = SecureRandom.hex(16)
            end while Member.exists?(auth_token: self.auth_token)
        end

end

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我使用has_secure_password作为会员模型。在编辑表单中,我忘记添加密码和password_confirmation来验证用户。

愚蠢的错误。