Search code examples
ruby-on-railsrubybuttoninputfilefield

Button as a file field in rails


I need a button that can be fully customised via css to prompt the user to select a file for upload when clicked. Does anyone know how to do this? Thanks


Solution

  • You'll want an input of file type, you can just add the field to your markup:

    <input type="file" name="name_of_attribute" />
    

    You can hook into it using css:

    input[type='file'] {
      height: 9001px;
    }
    

    There are methods in rails to generate the markup for you, have a look at the file_field method http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-file_field or file_field_tag method here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-file_field_tag

    Make sure you make your form multipart or you'll have problems uploading the file using the input.