method
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 { :action => "post" }, { :multipart => true } %> <label for="file">File to Upload</label> <%= file_field_tag "file" %> <%= submit_tag %> <%= end_form_tag %>
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.
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" />
Register or
log in
to add new notes.
amasses -
August 9, 2011
1 thank
HTML5 support
To support multiple file selection simply pass :multiple => true in the options hash:
file_field_tag 'file', :accept => 'image/jpeg', :multiple => true
ostapische -
October 19, 2015
0 thanks
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