Carrierwave无法上传 – 未显示错误

我整晚都在努力,这没有任何意义。 我正在改编一个旧的照片网络应用程序,以便在其中添加相册。 我制作了“失败”(基本上是图片)相册的嵌套资源。 我使用carrierwave将文件上传到S3存储桶。

奇怪的是:上传对于专辑模型(专辑图片)完全正常,但不会为故障模型上传。

我不明白为什么它现在是一个嵌套资源的问题。 显示它并不是一个问题,因为它出于某种原因,它通过forms正常,传递validation正常,没有错误被抛出,它重定向到失败#index成功,但数据库或S3中没有任何内容。

代码如下。 所有代码都在https://github.com/spq24/failboard

失败模型

class Fail  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  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

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

Debug Hash(当我尝试上传时会变为红色,但我看不到任何可能导致错误的内容)

这是调试信息:

 {"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=#>, "description"=>"", "tag_list"=>"test"}, "commit"=>"Create Fail", "controller"=>"fails", "action"=>"index"} 

如果还有其他需要请告诉我,我会把它放在这里。 谢谢你的帮助!

您是否尝试过validation失败映像的完整性或处理 ?

 validates_integrity_of :avatar validates_processing_of :avatar validates_download_of :avatar 

默认情况下,它会无声地失败,这有点糟糕。

我还建议尝试在rails控制台中创建记录,这有助于将问题隔离到模型或视图/控制器层。 在你的情况下,这看起来像:

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