Rails:具有多个占位符图像的回形针? [英] Rails: Paperclip with multiple placeholder images?

查看:103
本文介绍了Rails:具有多个占位符图像的回形针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的应用程序中为用户提供化身,并且我将其设置为用户可以使用Paperclip上传图像的地方.回形针具有很好的默认功能,您可以在用户未上传图像时定义占位符图像.我想知道的是,在创建相关记录时,是否有任何方法可以创建一组占位符图像并让回形针随机选择一个? IE,因此不是所有的无头像"图标都必须相同吗?

I've been trying to experiment with having avatars for users in my app, and I've set this up where a user can upload an image using Paperclip. Paperclip has a nice default functionality where you can define a placeholder image when the user has not uploaded an image. What I'm wondering is, is there any way to create a set of placeholder images and have paperclip choose one at random when the associated record is created? IE so not all the "no avatar" icons have to be identical?

谢谢!

推荐答案

关于如何完成此操作的最佳猜测是替代"您访问头像图像的方式.

My best guess for how to accomplish this would be to "override" how you access your avatar images.

也许是以下情况:

 module UserHelper

   def avatar_url(user)
     user.avatar ? user.avatar.url : random_avatar_url
   end

   def random_avatar_url
     ...
   end

 end

这样,您可以使用一个界面来访问现有的头像或从视图中随机选择一个.您不想直接在视图中使用random_avatar_url,因此可以将其设为私有或加以保护以确保其他人知道.

This way you can use one interface to access the existing avatar or a randomly chosen one from your views. You don't want to use random_avatar_url directly from your views, so maybe private or protect it to make sure others know.

这篇关于Rails:具有多个占位符图像的回形针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆