Validate value in text_area

Issue

I have a text_area in my rails app where users can paste plain text or code. I really don’t want to ask the user to choose text or code for me but want to make it like a WYSWYG text area

  • Right now I use pre tag. This renders code comments ok but makes text comments look visually ugly.
  • I can use a syntax highlighting gem, but this requires me to know that the pasted text is code.
  • Q: Is there any inbuilt apis in rails/ruby to validate if the value in a text area is code or text?

    <% if !comment.content.blank? %>    
                   <p> <pre> <%= simple_format comment.content  %></pre>  </p>
    <% end %>
    

Solution

You can instruct the user to wrap the code section around a specific keyword..for instance {code}
then in your template you can extract and decorate the the code section:

<p> <pre><%= comment.content.scan(/{code}(.*?){code}/m)  %></pre></p>

Answered By – Alireza

Answer Checked By – Cary Denson (AngularFixing Admin)

Leave a Reply

Your email address will not be published.