我没有用CarrierWave将照片保存在数据库中



我正在使用CarrierWave gem,我的问题是它不会将我保存在数据库中,它只会将我上传到uploads文件夹,我已经按照文档的要求在模型中进行了很好的配置。默认的图像是配置好的,它完美地显示了我。所有其他数据都保存了,除了照片。在控制器中,允许在参数中使用字符串类型。在我的环境中添加要求'carrierwave/orm/activerecord'。rb文件我不知道是什么错误。

我的模型:

class Profile < ApplicationRecord
belongs_to :user
mount_uploader  :foto,  FotoUploader
end

我的控制器:

def profile_params
params.require(:profile).permit(:foto, :titulo, :matricula,
:biografia, :telefono, :whatsapp, :provincia, :localidad,
:direccion, :cpostal, :idiomas, :areas, :edades, :escuelas,
:urgencias, :osociales, :tdomicilio, :presencial, :online,
:precioconsulta, :formadepago, :cuit, :iva)
end

我的观点(编辑):

<div class="col-xl-4">
<%= f.file_field :foto %>
</div>

我的观点(显示):

<div class="avatar">
<%=image_tag @profile.foto.url%>
</div>

我的上传:

class FotoUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url(*args)
#   # For Rails 3.1+ asset pipeline compatibility:
ActionController::Base.helpers.asset_path('avatar.png')
#
#   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
#   # do something
# end
# Create different versions of your uploaded files:
version :thumb do
process resize_to_fill: [150, 150]
end
# Add an allowlist of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_allowlist
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
#   "something.jpg" if original_filename
# end
end
在终端:

Started PATCH "/profiles/2" for 127.0.0.1 at 2021-07-25 17:19:48 -0300
Processing by ProfilesController#update as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "profile"=>{"foto"=>#<ActionDispatch::Http::UploadedFile:0x0000558e5e0b5d78 @tempfile=#<Tempfile:/tmp/RackMultipart20210725-126824-s6f1iw.png>, @original_filename="foto.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="profile[foto]"; filename="foto.png"rnContent-Type: image/pngrn">, "titulo"=>"Licenciada en psicologia", "matricula"=>"44986", "biografia"=>"", "telefono"=>"+543794438480", "whatsapp"=>"+543795859634", "provincia"=>"Corrientes", "localidad"=>"capital", "direccion"=>"barrio pujol mz 21 casa 2", "cpostal"=>"3400", "ninos"=>"0", "adolecentes"=>"0", "adultos"=>"0", "ancianos"=>"0", "parejas"=>"0", "grupos"=>"0", "psicoanalisis"=>"0", "cognitivo"=>"0", "integrativo"=>"0", "humanista"=>"0", "sistemico"=>"0", "es"=>"0", "en"=>"0", "pt"=>"0", "fr"=>"0", "it"=>"0", "ge"=>"0", "s1"=>"0", "s2"=>"0", "s3"=>"0", "presencial"=>"1", "online"=>"0", "osociales"=>"con reintegro", "tdomicilio"=>"No", "urgencias"=>"No", "precioconsulta"=>"", "fp1"=>"0", "fp2"=>"0", "fp3"=>"0", "cuit"=>"20323591185", "iva"=>"Monotributista"}, "commit"=>"GUARDAR", "id"=>"2"}

可能是我没有保存,因为foto字段生成超过255个字符和字符串字段只接受255?

如果您的上传程序使用storage :file,那么将文件保存到文件系统是预期的行为。有一些gem提供数据库存储。例如载波-postgresql。大多数使用载波的人不希望将文件存储在本地文件系统中,将其与云存储系统(如S3)一起使用

问题是没有安装ImageMagick。问题解决了

最新更新