Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit only images to be uploaded through file_field tag?

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

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

<% end %>

I have this form. I want to upload only images through this upload. How to do this?

like image 743
Carsen Avatar asked Jun 05 '12 11:06

Carsen


2 Answers

HTML5

<%= f.file_field :file, multiple: true, accept:'image/*' %>
like image 65
Alex I Avatar answered Nov 03 '22 01:11

Alex I


You can use paperclip gem to manage file attachments. It has validates_attachment validator which would be helpful.

validates_attachment :avatar, :presence => true,
  :content_type => { :content_type => "image/jpg" },
  :size => { :in => 0..10.kilobytes }
like image 23
Flexoid Avatar answered Nov 03 '22 01:11

Flexoid