2

Has anybody found a tip how to translate a simple_form input field to upload files ? I have a pretty simple form like that (in slim):

= simple_form_for(@some_object) do |f|
  = f.error_notification

  = f.input :text
  = f.input :photo, as: :file

  = f.button :submit

And the generated HTML always has a button 'Choose a file' with a label on the right 'No file chosen'. Googling gave nothing working, most of them proposing tricks in JS. Any other ideas ?

Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
belgoros
  • 3,590
  • 7
  • 38
  • 76
  • There are hacks http://stackoverflow.com/questions/1944267/how-to-change-the-button-text-of-input-type-file . – Arup Rakshit Jan 28 '16 at 11:25
  • Thank you for sharing, Igor. Unfortunately, none of the posted solutions talked about translations, all the labels and texts were hard coded in JS. I found this one among the Foundation building blocks: http://zurb.com/building-blocks/file-upload-button. I'd have just to find a way how replace Rails input file helper with that HTML code. – belgoros Jan 28 '16 at 14:59

1 Answers1

0

Apparently, there is no easy way to do this. According to the StackOverflow answer, this is not as Rails issue it's an HTML issue.

The way I solved the issue in Simple Form was by adding a label, and hiding the input field.

<%= simple_form_for(@user) do |f| %>
  <%= f.label :photo, "Your label here", class: "btn btn-default" %>
  <%= f.input :photo, as: :file, label: false, input_html: { style: "display: none" }%>
  <%= f.button :submit %>
<% end %>

When tou click the text in the label it will trigger the upload file, opening the window to select the file.

You can then add extra JavaScript to change the label text by the name of the selected file.