0

I have a form where user can enter a note via a textfield.

When entering the note, user can press 'enter' to add line breaks and also the note entered can consist of multiple lines and it is stored in a table column of type varchar(2000) latin1_swedish_ci

On some other page, I am retrieving the notes from the database and storing them in an array. I am alerting the array contents to test whether everything is ok

My problem is that if the note spans on more than 1 line or contains line breaks, it does not seem to be stored in that array and the alert function does not alert anything for that particular note.

However, it is displayed properly if i display it using plain ruby code.

Here is an example of my code:

/********** loop through the resultset and store the values in the test_array **********/
<% @saved_note.each do |note| %>
    <script type="text/javascript">
        testarray[note_counter] = '<%= note.value %>';
        alert(testarray[note_counter]);
        note_counter++;
    </script>
    <%= note.value %> 
<% end %> 

Any suggestion is most appreciated.

Thanks a lot

tanya
  • 2,925
  • 7
  • 32
  • 49

3 Answers3

2

If note.value spans more than one line, you will have this result:

testarray[note_counter] = 'Dear tanya,
how are you doing today?
sincerely, 
user';

The problem here is that javascript doesn't support multiline strings without some massaging. If you've got any kind of newline characters hanging out in note.value, you'll want to escape them (see this example) before printing them out.

Community
  • 1
  • 1
rjz
  • 16,182
  • 3
  • 36
  • 35
1

You can try

testarray[note_counter] = <%= raw note.value.to_json %>;

Personnaly I store that in AplicationHelper

def json value
  raw value.to_json
end

and then in your view

testarray[note_counter] = <%= json note.value %>;
Thomas Guillory
  • 5,719
  • 3
  • 24
  • 47
0

I'm unable to find an exact example of how I dealt with that in php, but the idea would be to try to parse all the symbols to their ansi code and check for the needed symbol like that. Just an idea tho.

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78