Carrierwave未上传 - 显示没有错误 [英] Carrierwave not uploading - no error shown

查看:135
本文介绍了Carrierwave未上传 - 显示没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在这一夜,这是没有意义的。我适应了老照片的Web应用程序有专辑在里面。我做了失败(基本图像)的专辑嵌套资源。我使用carrierwave将文件上传到S3桶。

I've been working on this all night and it makes no sense. I'm adapting an old photo web app to have albums in it. I made "fails" (basically images) a nested resource of albums. I am using carrierwave to upload files to an S3 bucket.

奇怪的是:上传工作完全正常的相册样板(专辑图片),但它并没有上载故障模型

我不明白为什么它会成为一个问题,这是一个嵌套的资源了。这是不是显示它是由于某种原因,它通过形式很好,通过验证罚款的问题,不会引发任何错误,它重定向到失败#的索引,比如它是成功的,但并没有什么在DB或S3。

I don't see why it'd be a problem that it's a nested resource now. It's not a problem displaying it's that for some reason, it goes through the form fine, passes validations fine, no errors are thrown, it redirects to fails#index like it was successful, but there is nothing in the db or in S3.

code是如下。所有code在 https://github.com/spq24/failboard

Code is below. All code at https://github.com/spq24/failboard

失效模型

class Fail < ActiveRecord::Base
  attr_accessible :description, :image, :remote_image_url, :fail_title, :tag_list,    :processed, :youtube_url, :album_id
make_voteable
acts_as_taggable

belongs_to :album

mount_uploader :image, ImageUploader


validates            :description, length: { :maximum => 200 }
validates            :album_id, presence: true
validates            :image, presence: true
    validates            :fail_title, presence: true, length: { :maximum => 50 }
    validate                   :maximum_amount_of_tags


def maximum_amount_of_tags
    number_of_tags = tag_list_cache_on("tags").uniq.length
    errors.add(:base, "Please only add up to 5 tags") if number_of_tags > 5
end

before_save :update_attachment_attributes

def update_attachment_attributes
  if image.present? && image_changed?
    self.content_type = image.file.content_type
    self.file_size = image.file.size
  end
end

def next
    user.fails.where("id > ?", id).order("id ASC").first
end

def prev
    user.fails.where("id < ?", id).order("id DESC").first
end


end

相册样板

class Album < ActiveRecord::Base
attr_accessible :name, :image, :image_url, :created_at

belongs_to :user
  has_many :fails, dependent: :destroy


  mount_uploader :image, ImageUploader


validates            :user_id, presence: true
validates            :image, presence: true
validates            :name, presence: true, length: { :maximum => 50 }



    before_save :update_attachment_attributes

    def update_attachment_attributes
      if image.present? && image_changed?
        #self.content_type = image.file.content_type 
        #self.file_size = image.file.size
      end
    end

  def next
    user.fails.where("id > ?", id).order("id ASC").first
end

def prev
    user.fails.where("id < ?", id).order("id DESC").first
end

end

失败控制器

 def new
   @fail = Fail.new(:album_id => params[:album_id])

   respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @fail }
  end
end

 def create
   @fail = Fail.new(params[:fail])

   respond_to do |format|
     if @fail.save
       format.html { redirect_to  @fail.album,  notice: 'You added a new photo!' }
       format.json { render json: @fail, status: :created, location: @fail }
     else
       format.html { render action: "new" }
       format.json { render json: @fail.errors, status: :unprocessable_entity }
     end
   end
 end

routes.rb中

routes.rb

resources :albums do
 get 'tags/:tag', to: 'fails#index', as: :tag
 resources :fails do
   member do
     post :up_vote
   end
 end

调试哈希(这会变成红色,当我尝试上传,但是我看不到任何会导致错误)

Debug Hash (this turns red when I try to upload, but I don't see anything that would cause the error)

下面是调试信息:

{"utf8"=>"✓",   "authenticity_token"=>"Hz6Gl95ultYDNIEjQioIckB8JXQwhiMxXIM9jrfqd5Q=", "fail"=>{"fail_title"=>"tester", "image"=>#<ActionDispatch::Http::UploadedFile:0x56195e8 @original_filename="pic19.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"fail[image]\"; filename=\"pic19.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/Kinertia/AppData/Local/Temp/RackMultipart20131125-10428-m2ktp2>>, "description"=>"", "tag_list"=>"test"}, "commit"=>"Create Fail", "controller"=>"fails", "action"=>"index"}

如果还有什么事需要请让我知道,我会放在这里。谢谢所有帮助!

If there is anything else needed please let me know and I will put it here. Thank you for all the help!

推荐答案

您是否尝试过<一个href="https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Validate-uploads-with-Active-Record"相对=nofollow>验证失败图片的完整性和处理?

Have you tried validating the integrity or processing of the fail image?

validates_integrity_of :avatar
validates_processing_of :avatar
validates_download_of :avatar

默认情况下它会自动失败,这有点吸。

By default it fails silently, which kinda sucks.

我也建议尝试创建的轨道控制台,它可以帮助找出问题到模型或视图/控制器层的纪录。在你的情况下,这看起来是这样的:

I also recommend trying to create the record in the rails console, which can help to isolate the problem to either the model or view/controller layers. In your case this would look something like:

Fail.create!(
  image: File.open('path/to/known/file.jpg'),
  album_id: 1,
  fail_title: 'Title'
)

这篇关于Carrierwave未上传 - 显示没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆