file_field_tag
file_field_tag(name, options = {})
public
Creates a file upload field. If you are using file uploads then you will also need to set the multipart option for the form tag:
<%= form_tag '/upload', multipart: true do %> <label for="file">File to Upload</label> <%= file_field_tag "file" %> <%= submit_tag %> <% end %>
The specified URL will then be passed a File object containing the selected file, or if the field was left blank, a StringIO object.
Options
-
Creates standard HTML attributes for the tag.
-
:disabled - If set to true, the user will not be able to use this input.
-
:multiple - If set to true, *in most updated browsers* the user will be allowed to select multiple files.
-
:accept - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.
Examples
file_field_tag 'attachment' # => <input id="attachment" name="attachment" type="file" /> file_field_tag 'avatar', class: 'profile_input' # => <input class="profile_input" id="avatar" name="avatar" type="file" /> file_field_tag 'picture', disabled: true # => <input disabled="disabled" id="picture" name="picture" type="file" /> file_field_tag 'resume', value: '~/resume.doc' # => <input id="resume" name="resume" type="file" value="~/resume.doc" /> file_field_tag 'user_pic', accept: 'image/png,image/gif,image/jpeg' # => <input accept="image/png,image/gif,image/jpeg" id="user_pic" name="user_pic" type="file" /> file_field_tag 'file', accept: 'text/html', class: 'upload', value: 'index.html' # => <input accept="text/html" class="upload" id="file" name="file" type="file" value="index.html" />
HTML5 support
To support multiple file selection simply pass :multiple => true in the options hash:
file_field_tag 'file', :accept => 'image/jpeg', :multiple => true
Multiple files
To use multiple file upload need to use variable_name[].
like this:
file_field_tag 'files[]', :multiple => true
and in controller:
if !params[ :files ].nil? params[ :files ].each{ |file| # do your staff } end