Preserving whitespace / indentation in Rails database column -
i have blocks of pseudo-code text stored in database printed off line-by-line , displayed in html.
pseudo-code entered in text column in database:
while (counter <= 10) { printf("enter grade: "); scanf("%d", &grade); total = total + grade; counter = counter + 1; } /* end while */
controller:
def index @code = code.first // filler time being end
index:
- @code.cont.each_line |line| - += 1 .line %span= %li.line= line - = 0
somewhere along way ruby automatically strips out whitespace , leaves me text. i'd know how preserve
this:
while (counter <= 10) { printf("enter grade: "); scanf("%d", &grade); total = total + grade; counter = counter + 1; } /* end while */
doesn't come out this:
while (counter <= printf("enter grade: "); scanf("%d", &grade); total = total + grade; counter = counter + 1; } /* end while */
i'm pretty sure space characters there, can't see them due way html handles whitespace. double check putting text variable , dumping console simple puts my_chunk_of_text
. if spaces there (and can't see why wouldn't be, based on code posted), have couple of alternatives:
1) sandwich displayed text <pre>
tag, renders preformatted text. you're going want in view, this:
<pre><%= @my_chunk_of_text %></pre>
2) save text in html format, non-breaking spaces (
) wherever want space appear, , <br>
need newline. code can rendered in view html_safe
helper.
<%= @my_chunk_of_text.html_safe %>
Comments
Post a Comment