brad
brad

Reputation: 9773

Why is my nested text_area helper adding html tags?

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?

Upvotes: 2

Views: 189

Answers (2)

brad
brad

Reputation: 9773

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

Upvotes: 2

Ryan Bigg
Ryan Bigg

Reputation: 107718

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

Upvotes: 1

Related Questions