【问题标题】:Reload Paperclip file uploads when validation fails验证失败时重新加载回形针文件上传
【发布时间】:2014-05-17 06:19:49
【问题描述】:

我是 Rails 新手。我目前正在为 Rails 3 中的模型设置 Paperclip。当其中一个表单字段验证失败时,它无法再次重新加载我上传的图像。它要求用户重新上传。它看起来对用户不友好。

我想做两件事来解决这个问题。如果所有字段都正确填写,我想将它存储在我的应用程序中(像往常一样回形针的系统文件夹)。如果字段验证失败,希望将图像临时存储在单独的文件夹中,直到它被保存。

我走在正确的道路上吗?否则有什么简单的方法可以做到这一点?

【问题讨论】:

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


    【解决方案1】:

    很遗憾,Paperclip 仅在成功保存包含文件的模型后才会保存上传的文件。

    我相信最简单的选择是使用 javascript 进行客户端验证,因此不需要所有后端配置/黑客攻击。

    【讨论】:

      【解决方案2】:

      我不得不在最近的一个项目中解决这个问题。这有点hacky,但它有效。我尝试在模型中使用 after_validation 和 before_save 调用 cache_images() 但由于某种我无法确定的原因创建失败,所以我只是从控制器调用它。希望这可以节省其他人一些时间!

      型号:

      class Shop < ActiveRecord::Base    
        attr_accessor :logo_cache
      
        has_attached_file :logo
      
        def cache_images
          if logo.staged?
            if invalid?
              FileUtils.cp(logo.queued_for_write[:original].path, logo.path(:original))
              @logo_cache = encrypt(logo.path(:original))
            end
          else
            if @logo_cache.present?
              File.open(decrypt(@logo_cache)) {|f| assign_attributes(logo: f)}
            end
          end
        end
      
        private
      
        def decrypt(data)
          return '' unless data.present?
          cipher = build_cipher(:decrypt, 'mypassword')
          cipher.update(Base64.urlsafe_decode64(data).unpack('m')[0]) + cipher.final
        end
      
        def encrypt(data)
          return '' unless data.present?
          cipher = build_cipher(:encrypt, 'mypassword')
          Base64.urlsafe_encode64([cipher.update(data) + cipher.final].pack('m'))
        end
      
        def build_cipher(type, password)
          cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC').send(type)
          cipher.pkcs5_keyivgen(password)
          cipher
        end
      
      end
      

      控制器:

      def create
        @shop = Shop.new(shop_params)
        @shop.user = current_user
        @shop.cache_images
      
        if @shop.save
          redirect_to account_path, notice: 'Shop created!'
        else
          render :new
        end
      end
      
      def update
        @shop = current_user.shop
        @shop.assign_attributes(shop_params)
        @shop.cache_images
      
        if @shop.save
          redirect_to account_path, notice: 'Shop updated.'
        else
          render :edit
        end
      end
      

      查看:

      = f.file_field :logo
      = f.hidden_field :logo_cache
      
      - if @shop.logo.file?
        %img{src: @shop.logo.url, alt: ''}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多