为什么我的嵌套text_area助手添加html标签? [英] Why is my nested text_area helper adding html tags?
问题描述
我在一个复杂的表单中有一个text_area,这个复杂的表单被称为是这样的
<%= f.fields_for :笔记do | notes_form | %GT;
<%= render:partial => 'note',:locals => {:f => notes_form,:operation => f,:count => operation.notes.count}%>
<%end%>
< p><%= add_child_linkAdd note,:operation_notes%>< / p>
并且部分看起来像这样
<%count || = 2%>
< div class ='fields'>
<%= f.text_area:note_text,:rows => 4,:class => 笔记%>
<%= remove_child_linkx,f,count%>
< / div>
表单上可以有很多注释,因此可以添加和移除子链接。
我遇到的问题是,如果我用文本'abcd'
添加记事,当我调出编辑表单我得到'< p> abcd< / p>'
。如果笔记中有换行符,它会添加< br />
标签。 text_area表单助手似乎使用simple_format助手,但我不知道为什么。任何人都可以帮忙,因为这是非常不受欢迎的行为?
之前在同一页面上,我显示注释并使用simple_format将其格式化为
<%= simple_format note .note_text%>
看起来simple_format有点破坏性,因为在这之后,对note.note_text的调用总是返回格式化文本。如果我将上面的内容更改为
<%= simple_format note.note_text.dup%>
那么note_text属性就不会改变,我可以得到相应的结果。
我将不得不更加关注simple_format,但这真的让我感到不受欢迎的行为。
编辑 / p>
看起来这已经在Rails 3.1中得到了修正
I have a text_area in a partial in a complex form that is called like so
<%= f.fields_for :notes do |notes_form| %>
<%= render :partial => 'note', :locals => {:f => notes_form, :operation => f, :count => operation.notes.count} %>
<% end %>
<p><%= add_child_link "Add note", :operation_notes %></p>
and the partial looks like this
<% count ||= 2 %>
<div class='fields'>
<%= f.text_area :note_text, :rows => "4", :class => "notes" %>
<%= remove_child_link "x", f, count %>
</div>
There can be many notes on the form hence the add and remove child links.
The issue I'm having is that if I add a note with the text 'abcd'
, when I bring up the edit form I get '<p>abcd</p>'
. If there are line breaks in the note it adds <br />
tags. The text_area form helper seems to be using the simple_format helper but I have no idea why. Can anyone help as this is very undesirable behaviour?
Ah solved,
Earlier on the same page I was displaying the note and using simple_format to format it with
<%= simple_format note.note_text %>
It seems that simple_format is somewhat destructive as after this, a call to note.note_text always returns the formatted text. If I change the above to
<%= simple_format note.note_text.dup %>
then the note_text attribute is not altered and I get the appropriate results.
I will have to look more closely at simple_format but this really strikes me as undesirable behaviour.
EDIT
It looks like this has been corrected in Rails 3.1
这篇关于为什么我的嵌套text_area助手添加html标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!