Why is my nested text_area helper adding html tags?

走远了吗. 提交于 2019-12-11 00:43:48

问题


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?


回答1:


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




回答2:


I would suspect that you have something in your Note model that is processing the text. Check for callbacks in this model.



来源:https://stackoverflow.com/questions/8032187/why-is-my-nested-text-area-helper-adding-html-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!