Make a field optional in rails

后端 未结 3 596
[愿得一人]
[愿得一人] 2021-01-23 05:08

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.<

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-23 05:25

    I didn't use paperclip, but in general in Rails you can add condition to decide if validations should be run.

    validates_attachment_size :avatar, 
      :less_than => 1.megabytes, 
      :unless => "avatar.blank?"
    

    You should add similar condition to all validations that affects avatar. If you want know more take a look here.

提交回复
热议问题