【问题标题】:Carrierwave not uploading - no error shownCarrierwave 未上传 - 未显示错误
【发布时间】:2013-12-09 19:02:01
【问题描述】:

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

奇怪的是:对于专辑模型(专辑图片),上传工作得非常好,但对于失败模型却不能上传。

我不明白为什么它现在是嵌套资源会成为问题。显示它不是问题,因为某种原因,它通过表单很好,通过验证很好,没有抛出错误,它像成功一样重定向到失败#index,但在 db 或 S3 中没有任何内容。

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

调试哈希(当我尝试上传时它会变成红色,但我没有看到任何会导致错误的东西)

这里是调试信息:

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

如果还有什么需要请告诉我,我会放在这里。谢谢大家的帮助!

【问题讨论】:

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


    【解决方案1】:

    你试过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'
    )
    

    【讨论】:

    • 没有用:/我把它们和我的其余验证放在 fail.rb 那是他们应该去的地方对吗?我在问题中添加了调试信息
    • 我无法使用控制台创建它。回复如下:
    • 抱歉:=> #
    • 您可以尝试使用! 方法,这样您就可以看到哪里出了问题?见上文。
    • 我输入:Fail.create!(fail_title: "tester", image: "C:/profile.jpg", a> 但这最终把我踢出了控制台?
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2016-12-22
    • 1970-01-01
    • 2023-03-26
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    相关资源
    最近更新 更多