Haml将输入元素呈现为转义

时间:2012-05-08 07:01:03

标签: ruby ruby-on-rails-3 firefox haml

我喜欢哈哈;

= 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会按预期呈现此代码,但在Firefox中就是这样;

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

为什么元素会被转义。如果你检查haml,你可以看到我没有把它们放进去。

  • 它是从哪里来的?
  • 为什么会这样?

2 个答案:

答案 0 :(得分:4)

哇...我刚刚遇到过类似的问题。我的猜测是,如果你将nil传递给form_builder的fields_for,则不会使用html_safe返回hidden_​​input。要快速修复,请添加

-else
  =""
整个elsif阻止之后

答案 1 :(得分:0)

您必须在nil区块中返回fields_for以外的其他内容。

This issue has been fixed in Haml 4.0.4 by mattwildig的帮助下,

@lulalala。以下是对问题的解释:

  

#fields_for助手为每条记录生成一个隐藏的输入字段,并将其添加到捕获块的结果中。通常,capture的返回值为SafeBuffer,因此不会对其进行转义。但是,如果块没有向缓冲区写入任何内容,则Haml会从String返回文字空capture。隐藏的输入元素与此字符串相连,当它与其他条目组合时,由于它是String被添加到SafeBuffer,它将被转义。