0

我试图通过上传文件(文件名、大小)来显示可能发生的错误。我正在使用适用于 Ruby on Rails 的 Jquery Fiel Upload 的广告改编版本 我试图修改此代码截断,但我不能。

else
    format.html { render action: "new" }
    format.json { render json: @upload.errors, status: :unprocessable_entity }
end


我有以下验证,我希望 json 在发生错误时写入,所以是这样的:

else
    format.html { render action: "new" }
    format.json { render json: @upload.errors, status: :unprocessable_entity, error:"Error!" }
end

(它确实有效)并且我需要一个错误规范,该规范写在验证中:message

我的验证:

validates :upload_file_name,  :presence   => true,
                                :format     =>{:with => %r{\.(cel)$}i}

  validates_uniqueness_of :upload_file_name, :message => "File with this name is already in the database"

  validates :upload_file_size,  :inclusion  => {:in =>10.megabytes..20.megabytes}, :message =>"Too big or too small"

  def to_jq_upload
  {
      "name" => read_attribute(:upload_file_name),
      "size" => read_attribute(:upload_file_size),
      "url" => upload.url(:original),
      "delete_url" => upload_path(self),
      "delete_type" => "DELETE" 
  }
  end

end

和我唯一的javascript:

<script type="text/javascript" charset="utf-8">
  $(function () {
      // Initialize the jQuery File Upload widget:
      $('#fileupload').fileupload();
      // 
      // Load existing files:
      $.getJSON($('#fileupload').prop('action'), function (files) {
        var fu = $('#fileupload').data('fileupload'), 
          template;
        fu._adjustMaxNumberOfFiles(-files.length);
        console.log(files);
        template = fu._renderDownload(files)
          .appendTo($('#fileupload .files'));
        // Force reflow:
        fu._reflow = fu._transition && template.length &&
          template[0].offsetWidth;
        template.addClass('in');
        $('#loading').remove();
      });

  });
</script
4

1 回答 1

0

我不确定我是否理解您的问题,但是如果您需要将错误消息与'error'您的 json 响应中的键相关联,您可以这样做:

render json: {error: @json.errors.full_messages}, status: :unprocessable_entity
于 2012-10-27T19:12:38.323 回答