在 rails 中设置一个可选字段 [英] Make a field optional in rails

查看:37
本文介绍了在 rails 中设置一个可选字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用回形针将头像附加到用户,效果很好,但是当新用户尝试注册时,它抱怨头像 bieng 太小且类型不正确.

I use paperclip to attach an avatar to users, which works fine but when a new user attempts to register it complains about the avatar bieng too small and not of the right type.

这是我验证我的头像的方式:

This is how i validate my avatars:

validates_attachment_size :avatar, :less_than => 1.megabytes
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png', 'image/gif']

这是我尝试注册时遇到的错误.

This is the error i get when i try to register.

There were problems with the following fields:

* Avatar file size file size must be between 0 and 1048576 bytes.
* Avatar content type is not included in the list

有没有办法让头像可以是空白的?

is there anyway to make it so that avatar can be blank?

推荐答案

我不知道这是否可行,但请尝试:

I don't know if this is gonna work but try:

validates_attachment_size :avatar, :less_than => 1.megabytes, :if => avatar_changed?
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/png', 'image/gif'], :if => avatar_changed?

这篇关于在 rails 中设置一个可选字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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