简体   繁体   中英

Remove newline in textarea generated by HAML / Form Builder

I have a very simple Rails form.:

= form_for @object :remote => true do |form|
   = form.text_area :text, :class => 'form-control'

For a new object, with a nil text attribute, this generates:

<textarea class="form-control" name="object[text]" id="object_text"> </textarea>

The blank space in that is a newline:

(byebug) form.text_area :text, :class => 'form-control'
"<textarea class=\"form-control\" name=\"user_deactivation[reason_text]\" id=\"user_deactivation_reason_text\">\n</textarea>"

I strip out the leading and trailing spaces on save, so data wise, its not a big deal, but when the user clicks on this field, it appears indented.

在此处输入图片说明

Relevant software versions:

  • Rails 5.02
  • haml-4.0.7
  • The text area is in a bootstrap 3 dialog

Why is this newline being generated and how can I stop it?

You can use the '~' operator, which is like the '=' operator, but automatically runs find_and_preserve on the output.

Like this: = form_for @object :remote => true do |form| ~ form.text_area :text, :class => 'form-control' = form_for @object :remote => true do |form| ~ form.text_area :text, :class => 'form-control'

This is not an ideal answer, but it does work. I would like something better:

 = find_and_preserve(form.text_area :text, :class => 'form-control')

Can that be automatic? Is there a way to have the text_area helper not insert a newline?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM