Ruby on Rails活动存储验证不起作用

rkttyhzu  于 9个月前  发布在  Ruby
关注(0)|答案(1)|浏览(106)

我正在使用:

Rails 6.0.4.4

Ruby 3.0.0
我已经将gem'active_storage_validations'添加到我的项目中,并尝试使用它来验证附加的头像是图像

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

字符串
在我的注册表中

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


的数据
所有其他验证工作正常。

如果我在附加图像上添加文件类型或大小的验证,它仍然允许用户上传任何文件类型和大小

hts6caw3

hts6caw31#

我是active_storage_validation gem的维护者,我不确定你在这里期待什么。
首先,你需要限制用户在UI部分的输入,这是在接受HTML属性,即accept="image/png, image/jpg, image/jpeg"在你的输入元素。有了这个,你的用户将只能上传这些内容类型的文件,这将提高用户体验。
然后你将在模型上运行验证,active_storage_validation开始发挥作用。content_type验证器有以下保护子句(return true unless record.send(attribute).attached?(这里)),所以如果你不附加任何东西,它不会引发错误。这减少了发送的错误数量。
我认为添加accept HTML属性将解决大多数情况。
如果没有,我们可以在gem repo中讨论这个问题

相关问题