I have a Portfolio model that contains a comments field of type text, whose values are a set of paragraphs delimited by some combo of cr and nl characters. The objective is to extract the paragraphs into an array, wrap them in <p> tags, and rejoin them for output to the browser; so the focus in the code below is lines 6-8.
The problem I’m having is that the < and > characters are getting rendered as HTML entities < and >.
I’m new to Ruby and Rails, so my guess is that the reason this isn’t working as intended is because I’m probably taking an incorrect approach–and I’d like to know how an experienced Ruby coder would address this sort of situation. How do you insert HTML tags into content before you send it to a view? Or is that always a violation of the MVC model–in which case, what’s a correct way to solve this sort of problem?
Here’s the code:
1. module ApplicationHelper
2.
3. def portfolio_featured
4. @portfolios = Portfolio.all
5. @portfolios.each do |p|
6. paras = p.comments.split(/\r?\n\r?\n/)
7. paras.collect! { |p| "<p>" + p + "</p>" }
8. p.comments = paras.join
9. end
10. end
11.
12. end
take a look at simple_format TextHelper.
It is probably what you are looking for.