Maximum length on a textarea in Ruby on Rails

喜你入骨 提交于 2019-11-29 13:13:52
Shalom Craimer

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

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.

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

Will output this 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!

Rahul

Could it be due to a typo?

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

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".

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 :)

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