waldyr.ar
waldyr.ar

Reputation: 15214

Rails render string with blank spaces in html file

When I do something like:

<%= c.title %>

Rails render it like this:

"
        Title
    "

This behavior isn't the same if I wrap the string with a tag. Can someone please explain me why Rails acts like this?

UPDATE

Github's Repo link

Upvotes: 2

Views: 3403

Answers (4)

Liber
Liber

Reputation: 850

I got the same problem with you!

The solution for me is to change the file Encoding from UTF-8 to UTF-8 without BOM

I'm using sinatra, glad to share with you!

Upvotes: 0

Cluster
Cluster

Reputation: 5616

This has to do with ERB instead of Rails. When the template is processed it outputs any values from output ERB tags (<%= %>) to the output buffer and simply executes any other code.

Everything else is left untouched. Whitespace before and after the ERB tags is untouched. The most that you can do is to use - in the tags to have it strip some of the whitespace.

<!-- -->
<% for i in 1..10 %>
  <%= i -%>
<% end %>
<!-- -->

Will output

<!-- foo -->
  1  2  3  4  5  6  7  8  9  10<!-- foo -->

Note the two spaces that still show up before each number, take out that whitespace and it will look like one long number.

Compare this with

<% for i in 1..10 %>
  <%= i %>
<% end %>

Will output

<!-- -->
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
<!-- -->

Here's the output from your test page after select View Source from the context menu.

<!DOCTYPE html>
<html>
<head>
  <title>Testapp</title>
  <link href="/assets/application-1b13569e9620782f423d4cd3ce931750.css" media="all" rel="stylesheet" type="text/css" />
  <script src="/assets/application-9a717ea62eac3463d689b2ba0a4e85b4.js" type="text/javascript"></script>
  <meta content="authenticity_token" name="csrf-param" />
<meta content="/hfgtJZWzxaQ2d7txQMAt2b+21MWSTYcf6/2F7Pei1k=" name="csrf-token" />
</head>
<body>

<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
What&#x27;s wrong with me?

<!-- -->What&#x27;s wrong with me surrounded by html commet<!-- -->


</body>
</html>

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160191

I don't see the problem. Here's the rendered HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Testapp</title>
  <link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/home.css?body=1" media="all" rel="stylesheet" type="text/css" />
  <script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/home.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
  <meta content="authenticity_token" name="csrf-param" />
<meta content="cOfMPm5S/tCHCEHkeRTeQTITAiz800s+3Q4ZgNWCNlY=" name="csrf-token" />
</head>
<body>

<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
What&#x27;s wrong with me?


</body>
</html>

The relevant layout section:

<body>

<%= yield %>

</body>

Upvotes: 0

Intrepidd
Intrepidd

Reputation: 20878

You probably have some indentation in your layout file or another file that renders this view.

Your application.html.erb may look like this :

<html>
  <head>...</head>
  <body>
    <%= yield %> <!-- Some whitespaces at the beginning of this line -->
  </body>
</html>

Rails simply replaces the <%= %> tags with the value of the expression in it, nothing more.

Upvotes: 0

Related Questions