Search code examples
ruby-on-rails-3file-uploadform-helpers

adding size option to file_field tag in rails


ANSWERED

I actually found the answer while formulating the question but I'm posting it any way since some might find this useful(as said here: https://meta.stackexchange.com/questions/49922/should-i-continue-adding-a-question-if-i-have-found-the-answer-myself)

I'm having trouble adding size to a file field in ROR3. Here was my syntax:

= f.file_field :file, :size => "11"

this doesnt appear but creates an file input field with this:

<input type="file" name="soap_test_zip_file[file]" id="soap_test_zip_file_file">

now I KNOW I made it work before so I looked into some old code and found this:

= file_field_tag :file, :size => 11

which outputs this:

<input type="file" size="11" name="file" id="file">

which gives me the correct size, but the wrong file id and name. So I tried this:

<input type="file" size="11" name="soap_test_file_file" id="soap_test_file_file">

which gives me the RIGHT ID, but the WRONG NAME. Question is how do I reproduce that file_field but with the size?

I looked into this answer by Ryan Bigg btw: Problem showing the 'size' attribute for a 'file_field' using Ruby on Rails 3

and he's saying it's a cross browser thing that they render file fields differently. That is the case, but I would like to render a short file field IF the browser can handle it.


Solution

  • I used:

    = file_field_tag :soap_test_zip_file, {:name => 'soap_test_zip_file[file]', :size => 11}
    

    This made me override the name(for the controller) and the size