Ruby-on-rails 更改 simple_form 中字段允许的大小和最大长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5342477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:41:51  来源:igfitidea点击:

change size and maxlength allowed for a field in simple_form

ruby-on-railssimple-form

提问by Omnipresent

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_sizein initializers/simple_form.rbhowever, I don't want to change the size globally but just on few fields.

我知道有一个default_input_sizeininitializers/simple_form.rb但是,我不想在全局范围内更改大小,而只是在几个字段上更改。

How would I do this?

我该怎么做?

回答by Omnipresent

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

回答by Sergey Kishenin

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

尝试使用 <%= f.input :lastname, :input_html => {:size => 40, :maxlength => 4} %>

回答by Leo Lukin

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

或者尝试使用 CSS <%= f.input :lastname, :input_html => {:style => 'width: 250px'} %>

回答by Pranesha Bunsee

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

在 input_html 中添加 size 和 maxlength 对我没有影响。我使用的是“input_field”而不是“input”。所以以下工作:

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

回答by Dominic

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

您可能将输入宽度的 css 声明为“自动”。删除该声明,然后自定义。