开发者

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 t开发者_JAVA百科ype="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.


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


doesn't seem to work for me..

I thought of another alternative : jquery ..

$('#user_photo_photo').attr('size', 1);

bingo!!


If you want to not rewrite the name attribute you can do something like this :

keep your form

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

add an override for the file_field method

# lib/my_override.rb

module ActionView
  module Helpers
    module FormHelper   
      def file_field(object_name, method, options = {})
        InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("file", options.update({:size => options[:size]}))
      end
    end
  end
end

And load it through an initializer

# config/initializers/load_lib.rb    

require 'my_override'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜