开发者

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

开发者 https://www.devze.com 2023-02-05 17:32 出处:网络
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: bloc

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?


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.


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

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


Ya seems so but..

I thought of another alternative : jquery ..

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

bingo!!


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

0

精彩评论

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