Carrierwave无法上传 - 未显示错误

时间:2013-11-25 07:07:11

标签: ruby-on-rails ruby-on-rails-3 file-upload amazon-s3 carrierwave

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

奇怪的是:上传对于相册模型(专辑图片)完全正常,但不会上传失败模型。

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

代码如下。 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

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=#<File:C:/Users/Kinertia/AppData/Local/Temp/RackMultipart20131125-10428-m2ktp2>>, "description"=>"", "tag_list"=>"test"}, "commit"=>"Create Fail", "controller"=>"fails", "action"=>"index"}

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

1 个答案:

答案 0 :(得分:3)

你试过validating the integrity or processing of the fail image吗?

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