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 name="attachment" type="file" /> file_field_tag 'avatar', :class => 'profile-input' # => <input name="avatar" type="file" /> file_field_tag 'picture', :disabled => true # => <input name="picture" type="file" disabled="disabled" /> file_field_tag 'resume', :value => '~/resume.doc' # => <input name="resume" type="file" value="~/resume.doc" /> file_field_tag 'user_pic', :accept => 'image/png,image/gif,image/jpeg' # => <input name="user_pic" accept="image/png,image/gif,image/jpeg" type="file" /> file_field_tag 'file', :accept => 'text/html', :class => 'upload', :value => 'index.html' # => <input name="file" accept="text/html" type="file" value="index.html" />
Source Code
# File action_view/helpers/form_tag_helper.rb, line 188 def file_field_tag(name, options = {}) text_field_tag(name, nil, options.update("type" => "file")) end
<code/>and<pre/>for code samples.