file_field_tag

file_field_tag(name, options = {})
Instance Public methods

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" />
doc_ruby_on_rails
2015-06-20 00:00:00
Comments
Leave a Comment

Please login to continue.