2

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic. I'd been looking for a solution to this for awhile. Simple format is indeed destructive in that it screws with your form text_area helper.
1

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

1 Comment

Nice thought but my note model is only class Note < ActiveRecord::Base belongs_to :operations belongs_to :reviews end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.