【问题标题】:default image from localhost rather than loading from cloudinary来自本地主机的默认图像,而不是从 cloudinary 加载
【发布时间】:2014-03-03 06:12:06
【问题描述】:

我正在使用 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 提供的帮助器,用于从那里加载图像。

【问题讨论】:

  • 存储默认图片assets/images文件夹。
  • @Sampat 它存储在那里。问题是我只想写“user.avatar.filename”,如果没有上传图片,它会显示资产中的图片,否则应该从 cloudinary 中检索。

标签: ruby-on-rails cloudinary


【解决方案1】:

我解决了如下问题

首先,默认图像需要保存到 cloudinary。 例如

<%= 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;'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2014-07-16
    相关资源
    最近更新 更多