Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:25:08+00:00 2026-06-08T02:25:08+00:00

A html_safe method on user generated comment content really is not a good solution,

  • 0

A html_safe method on user generated comment content really is not a good solution, obviously. However, this was, so far, the only solution I could come up with to implement the following feature: I wanted to enable users to quote another comment simply by typing the iteration id of the other comment into the comment form like this “#14” (to quote comment 14 of that article). Then this #14 gets replaced with “[quoted_comment.content]” in the content output.

This is my code for it in the comment model:

def content_with_quotes
  if content.match(/(#([0-9]+))\s/)
    comment_content = content
    comment_content.scan(/(#([0-9]+))\s/) do
      if quoted_comment = Comment.where(article_id: self.article_id).where(iteration_id: $2).first
        if quoted_comment.created_at < self.created_at
          return comment_content.sub(/(#[0-9]+)\s/, "<i>'#{quoted_comment.content}'</i> ")
        end
      end
    end
  else
    return content
  end
end

Then in my comment view, I apply it with comment.content_with_quotes.html_safe and everything works fine.

So, this is what I want, and it works, but of course this html_safe method is a bad idea for user submitted content, since it might not be html safe.
Any suggestions on how to approach my feature without doing the html_safe method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:25:11+00:00Added an answer on June 8, 2026 at 2:25 am

    I’d consider using a white-list approach and use the HTML Sanitizer methods to clean your strings.

    See http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my biopsis Works : :plain console.log(#{escape_javascript(render(@job_charge).html_safe)}); Does not work ( but should? )
I have an object in my controller @data=<meta property=\og:url\ content=\<%=SITE_URL%><%=request.request_uri%>\ />.html_safe When I print
I have 2 methods like this in my user_helper.rb def full_name(user) if user.last_name? user.first_name
I have this link: <div id='up_arrow_div'> <%= link_to &uArr;.html_safe, video_votes_path( :video_id => video.id, :type
I have this helper method that I can't seem to clean up with content_tag
I use this method for doing malloc over a 2d array, my sources are
I have this bug Warning: date() [function.date]: It is not safe to rely on
In my controller method for handling a (potentially hostile) user input field I have
as we all know that html_safe work in controller and view i am sending
I've tried everything. Every combination of the helpers raw , html_safe to_json including some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.