How to fix a “value too long for type character varying(255)” error

最后都变了- 提交于 2021-01-28 09:11:36

问题


I'm trying to save a file so that I can upload it to stripe using CarrierWave, but I'm getting the error:

ERROR: value too long for type character varying(255)

and don't understand why as I followed the CarrierWave usage guide on GitHub.

This is my application:

class SplitterStripeServer < Sinatra::Base

  CarrierWave.configure do |config|
    config.root = File.dirname(__FILE__) + "/public"
  end

  post "/" do
    img = Image.new
    img.file = params[:file] #carrierwave will upload the file automatically
    img.save!
    redirect to("/")
  end

  get "/upload" do
    erb :upload
  end

  get "/" do
    @image = Image.find(1)
    erb :index
  end
end

This is the uploader:

class ImagesUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :file
end

This is the model:

class Image
  include DataMapper::Resource

  property :id, Serial

  mount_uploader :file, ImagesUploader
end

I feel like I'm missing something simple.


回答1:


You need to decrease a length of a file name. Override filename method and cut a file's basename e.g. to 250 characters.

class ImagesUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :file

  def filename
     "#{file.basename[0..250]}.#{file.extension}" if original_filename.present?
  end
end


来源:https://stackoverflow.com/questions/41642491/how-to-fix-a-value-too-long-for-type-character-varying255-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!