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

  • SEARCH
  • Home
  • 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 8287871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:09:35+00:00 2026-06-08T12:09:35+00:00

I know that rails automatically escapes characters like ‘<‘ or ‘&’, but this does

  • 0

I know that rails automatically escapes characters like ‘<‘ or ‘&’, but this does nothing for multiple spaces next to each other. I would like to escape everything, including spaces.

I understand that normally you don’t want to use &nbsp; and that you should use css instead. However, I’m trying to take user input and display it, so css isn’t feasible.

For example, I have the user input:     test    . When I display it with <%=@user_input%> in the view, the extra whitespace is displayed as a single space (though it appears correctly in the source).

Is there an easy way to escape the whitespace? Should I just use h @user_input and then replace all the spaces?

  • 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-08T12:09:35+00:00Added an answer on June 8, 2026 at 12:09 pm

    The whitespace isn’t removed. Browsers simply interpret multiple whitespace characters as a single space.

    You could convert each space to &nbsp; if you want:

    <%= raw @user_input.gsub(/\s/, "&nbsp;") %>
    

    You could alternatively replace each space with an empty <span class="whitespace"></span> tag, and then use CSS to style the whitespace ‘characters’ however you like.

    Finally, you can do this with only CSS too using the white-space: pre style (example below).

    • http://jsfiddle.net/G3VnY/

    Edit (to answer the follow-up in your comment)

    <%= raw h("this      is      a    sample   &  with   ampersand.").gsub(/\s/, "&nbsp;") %>
    

    This escapes the & as &amp; in the source (and will do similar for other HTML entities), and then does the " " to &nbsp; conversion.

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

Sidebar

Related Questions

So I know that Rails helps protect against sql injections when used like this:
Does anybody know how to create a rails application that use noSql database? We
Does anyone know of a library that works with Ruby 1.9.1 on Rails 3
I know that this sort of question has been asked here before, but still
I know that Rails Console and IRB can be run from Tools. But how
I know that Rails has sorting methods built into ActiveRecord, but I am just
I know that RAILS_ROOT is the old way of getting Rails.root and that it
I know that in Rails (3.2.2), I can route requests to / with the
I know that Phonegap has an event for back button, but it's only available
I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'

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.