Carrierwave not uploading - no error shown

2019-07-22 08:24发布

问题:

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.

the weird thing is: the upload works perfectly fine for the album model (album image), but it doesn't upload for the fail model.

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 is below. All code at https://github.com/spq24/failboard

Fail Model

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

Album Model

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

Fails Controller

 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 (this turns red when I try to upload, but I don't see anything that would cause the error)

Here is the debug info:

{"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!

回答1:

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'
)