如何限制只通过file_field标签上传的图片?

Car*_*sen 4 ruby forms ruby-on-rails

<%= form_for @model_name, :url => {:controller => 'controller_name', :action => 'index'},:html => {:multipart => true},:validate => true do |f| %>

<%= file_field 'upload', 'datafile'%>

<% end %>
Run Code Online (Sandbox Code Playgroud)

我有这个表格.我想通过此上传仅上传图片.这该怎么做?

小智 9

HTML5

<%= f.file_field :file, multiple: true, accept:'image/*' %>
Run Code Online (Sandbox Code Playgroud)


Fle*_*oid 1

您可以使用回形针gem 来管理文件附件。它有validates_attachment验证器,这会很有帮助。

validates_attachment :avatar, :presence => true,
  :content_type => { :content_type => "image/jpg" },
  :size => { :in => 0..10.kilobytes }
Run Code Online (Sandbox Code Playgroud)