Doe Doe - 1 month ago 8
Ruby Question

HAML converts my code into wrong HTML

I'm writing HAML code:

.col-xs-4.form-control= text_field_tag :q


But this converts to:

<div class="col-xs-4 form-control">
<input type="text" name="q" id="q">
</div>


How can I write HAML that it will convert it to:

<input type="email" class="form-control" id="q" placeholder="Email">


I cannot find any good documentation for that.

Answer

What you're asking for is actually this:

.col-xs-4.form-control
  = text_field_tag :q

That ends up nesting the text_field_tag HTML inside of the other <div> that you've declared implicitly.

The Rails view helpers like text_field_tag are unaware of what DOM context they're being called in, so if you want to add classes you have to pass those through as explicit arguments:

= text_field_tag(:q, nil, class: 'col-xs-4 form-control')