Ruby on Rails中textarea的最大长度

时间:2022-05-04 22:19:35

I tried applying the :maxlenght => 40 on a textarea on my form. But it didn't work out. Can we have a length limit on a textarea?

我尝试在我的表单上的textarea上应用:maxlenght => 40。但它没有成功。我们可以对textarea有长度限制吗?

The code for text area is

文本区域的代码是

<%= f.text_area :data,
                :rows => 2,
                :cols => 60 ,
                :maxlength => 140,
                :autocomplete => :off,
                :class => "textareabytes" %>

3 个解决方案

#1


13  

Just like Rahul said, there's no maxlength attribute for textarea in HTML. Only text input's have that.

就像Rahul所说的那样,HTML中的textarea没有maxlength属性。只有文字输入才有。

The thing you need to remember, is that RoR's text_area function (and all of RoR's HTML-generator functions) accept any argument you'll give them. If they don't recognized the parameter, then the'll just convert it to HTML.

你需要记住的是,RoR的text_area函数(以及所有RoR的HTML生成器函数)接受你将给予它们的任何参数。如果他们不识别参数,那么只需将其转换为HTML。

<%=f.text_area :data, :hellothere => "hello to you too"%>

Will output this HTML:

将输出此HTML:

<textarea name="data" hellothere="hello to you too"></textarea>

I know it's hard to remember, but Ruby on Rails isn't magic, it just does a lot of things for you. The trick is to know how it does them, so you can understand why they work, and how to fix them when they don't!

我知道这很难记住,但Ruby on Rails并不神奇,它只是为你做了很多事情。诀窍是知道它是如何做到的,所以你可以理解它们的工作原理,以及如何解决它们的问题!

#2


4  

Could it be due to a typo?

可能是由于拼写错误?

":maxlenght => 40 " in your post is misspelt.

你的帖子中的“:maxlenght => 40”拼写错误。

EDIT:

编辑:

I didn't read your post carefully. I think there is no maxlength attribute for textarea in HTML. You will have to handle it in JavaScript. There is more information in "MaxLength on a Textarea".

我没仔细看过你的帖子。我认为HTML中没有textarea的maxlength属性。您将不得不在JavaScript中处理它。 “Textarea上的MaxLength”中提供了更多信息。

#3


0  

Not strictly what you're after of course, but, you can always put a:

当然不是严格意义上的事情,但是,你可以随时提出:

validates_length_of :data, max: 40

on your model. Won't stop the textarea size of course :)

在你的模型上。当然不会停止textarea大小:)

#1


13  

Just like Rahul said, there's no maxlength attribute for textarea in HTML. Only text input's have that.

就像Rahul所说的那样,HTML中的textarea没有maxlength属性。只有文字输入才有。

The thing you need to remember, is that RoR's text_area function (and all of RoR's HTML-generator functions) accept any argument you'll give them. If they don't recognized the parameter, then the'll just convert it to HTML.

你需要记住的是,RoR的text_area函数(以及所有RoR的HTML生成器函数)接受你将给予它们的任何参数。如果他们不识别参数,那么只需将其转换为HTML。

<%=f.text_area :data, :hellothere => "hello to you too"%>

Will output this HTML:

将输出此HTML:

<textarea name="data" hellothere="hello to you too"></textarea>

I know it's hard to remember, but Ruby on Rails isn't magic, it just does a lot of things for you. The trick is to know how it does them, so you can understand why they work, and how to fix them when they don't!

我知道这很难记住,但Ruby on Rails并不神奇,它只是为你做了很多事情。诀窍是知道它是如何做到的,所以你可以理解它们的工作原理,以及如何解决它们的问题!

#2


4  

Could it be due to a typo?

可能是由于拼写错误?

":maxlenght => 40 " in your post is misspelt.

你的帖子中的“:maxlenght => 40”拼写错误。

EDIT:

编辑:

I didn't read your post carefully. I think there is no maxlength attribute for textarea in HTML. You will have to handle it in JavaScript. There is more information in "MaxLength on a Textarea".

我没仔细看过你的帖子。我认为HTML中没有textarea的maxlength属性。您将不得不在JavaScript中处理它。 “Textarea上的MaxLength”中提供了更多信息。

#3


0  

Not strictly what you're after of course, but, you can always put a:

当然不是严格意义上的事情,但是,你可以随时提出:

validates_length_of :data, max: 40

on your model. Won't stop the textarea size of course :)

在你的模型上。当然不会停止textarea大小:)