我喜欢哈哈;
= 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>
<input id="company_attachments_attributes_0_id" name="company[attachments_attributes][0][id]" type="hidden" value="114" /><input id="company_attachments_attributes_1_id" name="company[attachments_attributes][1][id]" type="hidden" value="115" /><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,你可以看到我没有把它们放进去。
答案 0 :(得分:4)
-else
=""
整个elsif阻止之后
答案 1 :(得分:0)
您必须在nil
区块中返回fields_for
以外的其他内容。
@lulalala。以下是对问题的解释:
#fields_for
助手为每条记录生成一个隐藏的输入字段,并将其添加到捕获块的结果中。通常,capture
的返回值为SafeBuffer
,因此不会对其进行转义。但是,如果块没有向缓冲区写入任何内容,则Haml会从String
返回文字空capture
。隐藏的输入元素与此字符串相连,当它与其他条目组合时,由于它是String
被添加到SafeBuffer
,它将被转义。