change size and maxlength allowed for a field in simple_form

拈花ヽ惹草 提交于 2020-06-08 04:43:26

问题


How can I change the size and max allowed characters for a field using Simple Forms. I've tried following but does not work:

<%= f.input :lastname, :size => 40, :max => 4 %>

I know there is a default_input_size in initializers/simple_form.rb however, I don't want to change the size globally but just on few fields.

How would I do this?


回答1:


<%= f.input :lastname, input_html: { maxlength: 15, size: 40} %>



回答2:


try to use <%= f.input :lastname, :input_html => {:size => 40, :maxlength => 4} %>




回答3:


Or try to use CSS <%= f.input :lastname, :input_html => {:style => 'width: 250px'} %>




回答4:


Adding size and maxlength in input_html had no effect for me. I am using "input_field" instead of "input". So the following worked:

<%= form.input_field :effective_from_date,
                     as: :string,
                     class: 'activate-datepicker',
                     maxlength: 11,
                     size: 11,
                     label: false %>



回答5:


It's possible you declared your css for input widths set to 'auto'. Remove that declaration, and then customize.



来源:https://stackoverflow.com/questions/5342477/change-size-and-maxlength-allowed-for-a-field-in-simple-form

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