Problem showing the 'size' attribute for a 'file_field' using Ruby on Rails 3

戏子无情 提交于 2019-12-11 03:33:35

问题


I am using Ruby on Rails 3 and I have a form like this:

<%= form_for(@user, ... ) do |f| %>
  ...
  <%= f.file_field :avatar, :id => "test_id", :style => "display: block", :size => "13" %>
  ...
<% end %>

When I go to see the source of the page, this is the HTML code generated:

<input type="file" style="display: block;" name="user[avatar]" id="test_id">

that means the 'size' attribute there isn't.

I tried on Firefox, Chrome and Safari: same output, but it seems to have everything set correctly.

Is it a problem related to RoR3?


回答1:


I actually found that the size attribute is intentionally left out of the Rails source code here:

https://github.com/rails/rails/blob/75366cb82dc6fa4b3dada2a450dda18496f3eddd/actionpack/lib/action_view/helpers/form_helper.rb#L734

"to_input_field_tag("file", options.update({:size => nil})"

Don't know why but glad to hear you found a workaround.




回答2:


Here is a workaround by specifying the size in the style.

<%= f.file_field :avatar, :style=>"width: 13px" %>



回答3:


Ya seems so but..

I thought of another alternative : jquery ..

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

bingo!!




回答4:


The size of a file field isn't configurable due to how browsers choose to render them and has nothing to do with Rails.



来源:https://stackoverflow.com/questions/4756636/problem-showing-the-size-attribute-for-a-file-field-using-ruby-on-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!