1

我正在使用 cloudinary 将用户头像存储在我的 rails 4 应用程序中。我的图像资产中还有一个占位符图像。我想知道如果用户没有上传他的头像,我如何从本地主机加载它。

截至目前,我必须将支票添加为

- if user.avatar.present?
  = cl_image_tag(user.avatar.filename, width: 46, height: 46)
- else
  = image_tag 'default.png', style: 'width:46px; height:46px;'

我可以将默认图像指定为

= cl_image_tag(user.avatar.filename, width: 46, height: 46, default: 'default.png')

但默认图像必须存储在 cloudinary 上。我不想将它存储在 cloudinary 上,因为 cloudinary 对数据传输收费。因此,我已将“default.png”存储在资产中。'cl_image_tag' 是 cloudinary 提供的帮助器,用于从那里加载图像。

4

1 回答 1

3

我解决了如下问题

首先,默认图像需要保存在云端。例如

<%= cl_image_tag("non_existing_id.png", width: 100, height: 100, default_image: "avatar.png") %>

如果您不想从 cloudinary 而是从我们的 localhost 加载默认图像,请在部分中添加以下代码并使用此部分。

- if user.avatar.present?
  = cl_image_tag(user.avatar.filename, width: 46, height: 46)
- else
  = image_tag 'noPic_80.png', style: 'width:46px; height:46px;'
于 2014-02-18T04:50:23.390 回答