Çağdaş
Çağdaş

Reputation: 1023

Haml renders input element as escaped

I have a haml like ;

= form_for @company, :html => {:multipart => true}, :url => update_user_company_path do |f|
  .field
    Title:#{f.text_field :name}
  = f.fields_for :attachments do |builder|
    - if builder.object.new_record?
      .field
        = builder.hidden_field :name, :value => 'logo'
        = builder.file_field :file
    - elsif builder.object.name.eql?('logo') && !builder.object.file.url.eql?('/files/original/missing.png')
      .field
        %span.thumbnail
          = link_to "Delete", delete_company_attachment_path(@company, builder.object), :method => :delete, :class => "remove_image"
          = image_tag builder.object.file.url, :style => "height:86px;width:125px"
  = f.submit 'Ok'

Chrome renders this code as intended, but in Firefox it is like;

<form method="post" id="edit_company_29" enctype="multipart/form-data" class="edit_company" action="/users/25/company" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"><input type="hidden" value="put" name="_method"><input type="hidden" value="thisismytokenvalue=" name="authenticity_token"></div>
  <div class="field">
    Title:<input type="text" value="sdgdfgghjh" size="30" name="company[name]" id="company_name">
  </div>
  &lt;input id="company_attachments_attributes_0_id" name="company[attachments_attributes][0][id]" type="hidden" value="114" /&gt;&lt;input id="company_attachments_attributes_1_id" name="company[attachments_attributes][1][id]" type="hidden" value="115" /&gt;<div class="field">
    <input type="hidden" value="logo" name="company[attachments_attributes][2][name]" id="company_attachments_attributes_2_name">
    <input type="file" name="company[attachments_attributes][2][file]" id="company_attachments_attributes_2_file">
  </div>
  <input type="submit" value="Ok" name="commit">
</form>

Why is an element escaped. If you check haml, you can see I didn't put them.

Upvotes: 1

Views: 955

Answers (2)

monozok
monozok

Reputation: 605

You must return something other than nil in the fields_for block.

This issue has been fixed in Haml 4.0.4 by mattwildig, with the help of @lulalala. Here is an explanation of the problem:

The #fields_for helper generates a hidden input field for each record, which it adds to the result of capturing the block. Normally the return value of capture will be a SafeBuffer, so this won’t be escaped. However if the block doesn’t write anything to the buffer then Haml returns a literal empty String from capture. The hidden input element is concatted to this string, and when it is combined with the rest of the entries, since it is a String being added to a SafeBuffer, it gets escaped.

Upvotes: 0

Cristian Bica
Cristian Bica

Reputation: 4117

Wow ... I've just had a similar issue. My guess is that if you pass nil to the form_builder's fields_for the hidden_input isn't returned with html_safe. To quickly fix that add a

-else
  =""

after the whole elsif block

Upvotes: 4

Related Questions