0

我正在使用回形针来管理个人资料图片上传和 css/js 的 jasny 引导程序。我可以上传个人资料图片并提交我的表单,而无需选择文件,没有任何问题。但是,当我选择一个文件并在提交表单之前将其删除时生成 NoHandlerError 时。

我对 javascript 不是很熟悉,但看起来这是正在生成的,因为 clear 函数有效地选择了一个名为“”(设置:avatar =>“”)的文件,而不是将其留空。

我的目标是能够选择一个文件,然后将其删除并能够提交表单。

提前非常感谢您的帮助。

用户.rb

class User < ActiveRecord::Base
  has_attached_file :avatar, styles: { :default => "x150>", :thumbnail => "100x100>" },
                        default_url: '/assets/missing_avatar.png'
end

_avatar.html.erb

<div class="fileupload fileupload-new" data-provides="fileupload">
  <div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"></div>
  <div>
    <span class="btn btn-file"><%= f.file_field :avatar %>
      <span class="fileupload-new">Select image</span>
      <span class="fileupload-exists">Change</span>
      <input type="file" />
    </span>
    <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
  </div>
</div>

bootstrap.file-upload.js

!function ($) {

  "use strict"; // jshint ;_

  var Fileupload = function (element, options) {
    this.$element = $(element)
    this.type = this.$element.data('uploadtype') ||     (this.$element.find('.thumbnail').length > 0 ? "image" : "file")

    this.$input = this.$element.find(':file')
    if (this.$input.length === 0) return

    this.name = this.$input.attr('name') || options.name

    this.$hidden = this.$element.find(':hidden[name="'+this.name+'"]')
    if (this.$hidden.length === 0) {
      this.$hidden = $('<input type="hidden" />')
      this.$element.prepend(this.$hidden)
    }

    this.$preview = this.$element.find('.fileupload-preview')
    var height = this.$preview.css('height')
    if (this.$preview.css('display') != 'inline' && height != '0px' && height != 'none')     this.$preview.css('line-height', height)

    this.$remove = this.$element.find('[data-dismiss="fileupload"]')

    this.$element.find('[data-trigger="fileupload"]').on('click.fileupload',     $.proxy(this.trigger, this))

    this.listen()
  }

  Fileupload.prototype = {

    listen: function() {
      this.$input.on('change.fileupload', $.proxy(this.change, this))
      if (this.$remove) this.$remove.on('click.fileupload', $.proxy(this.clear, this))
    },

    change: function(e, invoked) {
      var file = e.target.files !== undefined ? e.target.files[0] : (e.target.value ? { name: e.target.value.replace(/^.+\\/, '') } : null)
      if (invoked === 'clear') return

      if (!file) {
        this.clear()
        return
      }

      this.$hidden.val('')
      this.$hidden.attr('name', '')
      this.$input.attr('name', this.name)

      if (this.type === "image" && this.$preview.length > 0 && (typeof file.type !==     "undefined" ? file.type.match('image.*') : file.name.match('\\.(gif|png|jpe?g)$')) && typeof FileReader !== "undefined") {
        var reader = new FileReader()
        var preview = this.$preview
        var element = this.$element

        reader.onload = function(e) {
      preview.html('<img src="' + e.target.result + '" ' + (preview.css('max-height') != 'none' ? 'style="max-height: ' + preview.css('max-height') + ';"' : '') + ' />')
      element.addClass('fileupload-exists').removeClass('fileupload-new')
    }

        reader.readAsDataURL(file)
      } else {
        this.$preview.text(file.name)
        this.$element.addClass('fileupload-exists').removeClass('fileupload-new')
      }
    },

    clear: function(e) {
      this.$hidden.val('')
      this.$hidden.attr('name', this.name)
      this.$input.attr('name', '')
      this.$input.val('') // Doesn't work in IE, which causes issues when selecting the same file twice

      this.$preview.html('')
      this.$element.addClass('fileupload-new').removeClass('fileupload-exists')

      if (e) {
        this.$input.trigger('change', [ 'clear' ])
        e.preventDefault()
      }
    },

    trigger: function(e) {
      this.$input.trigger('click')
      e.preventDefault()
    }
  }
4

1 回答 1

1

我不知道解决这个问题的正确方法,但它有效:)

在控制器中

def update
  @user = User.find(params[:id])
  respond_to do |format|
      validate_avatar
      if @user.update_attributes(params[:usuario])
        sign_in @usuario
        format.html { redirect_to @user }
      else
        format.html { render action: "edit" }
      end
  end
end

只需添加此功能

def validate_avatar
  params[:user].delete 'avatar' if params[:user]['avatar'] = ""
end
于 2013-02-25T04:37:37.390 回答