2016-08-20 43 views
0

Heroku的控制台:我怎么渲染@使用JSON photo.errors(使用Rails的4 dropzone.js)

开始POST “/照片”
处理由PhotosController#创建为JSON
参数:{“utf8”=>“✓”,“authenticity_token”=>“D5pc72xeJ6J/g ==”,“photo”=> {“title”=>“fffffffffffffffffffffffffffffffffffffffffffffffffffff”,“tag_list”=> [“”] “picture”=>#,@ original_filename =“size.jpg”,@ content_type =“image/jpeg”,@ headers =“Content-Disposition:form-data; name = \”photo [picture] \“; filename = \“size.jpg \”\ r \ nContent-Type:image/jpeg \ r \ n“>},”null“=>”“,”commit“=>”上传“}
已完成500内部服务器错误941毫秒(ActiveRecord:13.9ms)
ArgumentError('#,@messages = {:title => [“太长(最多30个字符)”]}>'不是ActiveModel兼容对象。它必须实现:to_partial_path):
应用程序/控制器/ photos_controller.rb:40:在`块(2级)中创建”

PhotosController

def create 
    @photo = current_user.photos.build(photo_params) 
    respond_to do |format| 
     if @photo.save 
     format.html { redirect_to @photo, notice: 'Item was successfully created.' } 
     format.json { render json: @photo } 
     else 
     format.html { render 'new'} 
     **rb:40** (format.json { render @photo.errors, status: :unprocessable_entity } 
     end 
    end 
end 

dropzonephotos .js

$(document).ready(function() { 
    var dropzone; 
    Dropzone.autoDiscover = false; 
    dropzone = new Dropzone('#dropform', { 
    maxFiles: 2, 
    maxFilesize: 3, 
    paramName: 'photo[picture]', 
    headers: { 
     "X-CSRF-Token": $('meta[name="csrf-token"]').attr('content') 
    }, 
    addRemoveLinks: true, 
    clickable: '.dz-default.dz-message', 
    previewsContainer: '.dz-default.dz-message', 
    thumbnailWidth: 200, 
    thumbnailHeight: 200, 
    parallelUploads: 100, 
    autoProcessQueue: false, 
    uploadMultiple: false 
    }); 
    $('#item-submit').click(function(e) { 
    e.preventDefault(); 
    e.stopPropagation(); 
    if (dropzone.getQueuedFiles().length > 0) { 
     return dropzone.processQueue(); 
    } 
    else { 
     return $('#dropform').submit(); 
    } 

    }); 
    return dropzone.on('success', function(file, responseText) { 
    return window.location.href = '/photos/' + responseText.id; 
    }); 
    return dropzone.on('error', function(file, errorMessage, xhr) { 
    console.log('error'); 
    }); 
}); 

没有错误当验证失败时,在视图中呈现视图。在dropzone缩略图上方只有一个“X”,并在悬停“内部服务器错误”时显示一条消息。缩略图仍显示在视图中,但它实际上已被删除。如果我再次点击提交,表单将被处理为html,因为dropzone/no json中没有照片提交。

+0

请参阅我的回答:http://stackoverflow.com/questions/34820853/dropzone-js-and-form-validation/39059003#39059003 – Kaktusiarz

回答

0

所以我决定不用担心在表单内部使用Rails Active Record Validation Errors来做这件事,这可能是不可能的,因为表单由json提交并且Rails助手可能没有设置成可以工作方式,我决定只需在dropzone.on('error', function(file, errorMessage)内手动执行.js文件本身,方法是将错误附加到视图中。