'Ruby on rails Active Storage Validations not working

I am using:

Rails 6.0.4.4

Ruby 3.0.0

I have added gem'active_storage_validations' to my project, and am trying to use it to validate that attached avatars are images

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable, :trackable, :lockable, :masqueradable
         
  validates :first_name, :last_name, presence: true
  validates :email, uniqueness: true
  validates :terms_and_conditions, acceptance: true
  validates :avatar, attached: true,  content_type: ['image/png', 'image/jpg', 'image/jpeg']
  has_one_attached :avatar
end

in my sign_up form

<%= render "devise/shared/error_messages", resource: resource %>
<div class="container pt-3 pb-3">
  <div class="card">
    <div class="card-header">
      <h2>Sign up</h2>
    </div>
    <div class="card-body">
      <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
        <div class="field mb-3">
          <%= f.label :profile_picture %><br />
          <%= f.file_field :avatar, class: "form-control"  %>
        </div>
        <div class="field mb-3">
          <%= f.label :email %><br />
            <%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control", placeholder: "[email protected]" %>
        </div>
        <div class="field mb-3">
          <%= f.label :first_name %><br />
            <%= f.text_field :first_name, required: true, autofocus: true, autocomplete: "first_name", class: "form-control" %>
        </div>
        <div class="field mb-3">
          <%= f.label :last_name %><br />
            <%= f.text_field :last_name, autofocus: true, autocomplete: "last_name", class: "form-control" %>
        </div>
        <div class="field mb-3">
          <%= f.label :password %>
          <% if @minimum_password_length %>
          <em>(<%= @minimum_password_length %> characters minimum)</em>
          <% end %><br />
          <%= f.password_field :password, autocomplete: "new-password", class: "form-control" %>
        </div>
        <div class="field mb-3">
          <%= f.label :password_confirmation %><br />
          <%= f.password_field :password_confirmation, autocomplete: "new-password", class: "form-control" %>
        </div>
        <div class="form-check form-switch text-muted">
          <%= f.check_box :terms_and_conditions, class: 'form-check-input', id: 'flexSwitchCheckDefault' %>
          I hereby confirm I have read and undertood the terms and conditions
        </div>
        <div class="actions">
          <%= f.submit "Continue", class: "btn btn-primary"  %>
        </div>
      <% end %>
      <br>
    <%= render "devise/shared/links" %>
    </div>
  </div>
</div>

Eroor image

All the other validations work fine.

If I add validations for file type or size on the attached image, it still allows the user to upload any file type and size



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source