Reputation: 2066
I'm trying to program some forms that include text_area
tags in Rails 3.1.3. Generally these text areas are filled with more than one paragraph (i.e. they include line breaks). If I save the text and display it with simple_format
, the paragraphs display correctly, but if I repopulate the same form for editing, the format seems to add an indent after every line break. So, for example, if I input the following into the text area of the form:
Item 1
Item 2
Item 3
And I save it, the next time I load the form to edit the data, the text_area is populated with the following:
Item 1
Item 2
Item 3
Does anyone know why this happens and how to fix it? The segment of the form view that displays this follows:
<fieldset>
<section class="field">
<%= f.label "Títulos, seminarios, cursos" %>
<%= f.text_area :titles_seminars_courses %>
</section>
...
</fieldset>
I'm not doing anything special when saving to the database (no gsub or anything), just directly saving the 'Item 1\r\nItem 2\r\nItem 3\r\n'
string. I'm using the same form for the initial creation and edition of this data.
Thank you in advance for any help with this issue.
EDIT 1:
The view that displays the data correctly is the following (but it's not in a text_area):
<section class="titles_seminars_courses">
<%= simple_format(@academic_background.titles_seminars_courses, :class => "other_study") %>
</section>
EDIT 2:
<textarea cols="40" id="academic_background_titles_seminars_courses" name="academic_background[titles_seminars_courses]" rows="20">Item 1
Item 2
Item 3</textarea>
Upvotes: 1
Views: 650
Reputation: 1842
For HAML, If you use:
~ f.text_area
instead of:
= f.text_area
it has the same effect as Haml::Template.options[:ugly] = true
and will preserve the whitespace in your saved text string, rather than trying to pretty it up.
read more in the docs here
Upvotes: 0
Reputation: 2066
Ok, the problem seemed to be the mix between html.haml
and html.erb
views in the application. So either the .erb
views are not properly rendered, or they don't play nice with the other .haml
views. In mi case, for example, the application layout view is in haml, but the form was in erb.
Whatever the cause, the bottom line is that the text_area_tags don't render correctly in erb, but do in haml. So my workaround, although I know it is very hacky and not pretty, was to make a small partial in haml called _text_area.html.haml
which renders just the textarea for a form:
_text_area.html.haml:
-if defined? options
= f.text_area field, options
-else
= f.text_area field
So every time I want to have a text_area tag in a form, I have to call a render like so:
<%= form_for @instance do |f| %>
...
<%= render :partial => 'common/text_area', :locals => {:f => f, :field => :some_field_from_instance, :options => {:cols => 40, :rows => 10}} %>
<%= render :partial => 'common/text_area', :locals => {:f => f, :field => :some_other_field} %>
...
<% end %>
This renders perfectly. That's why I think the problem lies with haml and its interpretation of indentation. If anyone has a better solution, please do let me know.
Upvotes: 2