Maximum length on a textarea in Ruby on Rails

前端 未结 4 1518
花落未央
花落未央 2020-12-20 12:23

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?

The code for text are

相关标签:
4条回答
  • 2020-12-20 13:07

    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!

    0 讨论(0)
  • 2020-12-20 13:14

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

    0 讨论(0)
  • 2020-12-20 13:15

    You can use the maxlength attribute. It is new for the tag in HTML5. It should work nowadays.

    0 讨论(0)
  • 2020-12-20 13:17

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

    0 讨论(0)
提交回复
热议问题