I have = f.file_field "file", required: true, class: "filestyle"
in my html.slim in Rails. How do I make sure that only text files with extension .txt
can be uploaded, but not other files with other extensions like .png
or .jpg
Asked
Active
Viewed 836 times
0

gogofan
- 533
- 1
- 10
- 20
2 Answers
2
Use :accept
parameter of file_field
:
: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.
Mime type for text files is text/plain
. So your field would look like this:
= f.file_field "file", required: true, class: "filestyle", accept: "text/plain"

Sergio Tulentsev
- 226,338
- 43
- 373
- 367
-
What if I am not using a modal for that, is it still doable? – gogofan Aug 08 '17 at 11:50
-
@JeffreyYong: I don't see the word "modal" mentioned anywhere in this post or linked docs. So I'm not sure what you meant by that. – Sergio Tulentsev Aug 08 '17 at 17:18
-
Your solution actually already works for me. The file chooser prevents the user from choosing file type other than text file. Thanks! – gogofan Aug 08 '17 at 20:02
0
Try using Paperclip or carrierwave gem.This should make your life easier :)

Jishnu Janardhanan
- 63
- 1
- 1
- 6