0

I am focused on the wrong layer of abstraction here, but can't figure out where.

I have this file views/pages/overview.html.erb

<%= stylesheet_link_tag "cust/coderay"%>
<h1>Overview</h1>
<hr>
Here's my code test:

<%= html = CodeRay.scan("puts 'Hello, world!'", :ruby).div(:line_numbers => :table)%>

<hr>
Back <%=link_to "home", "home"%>.
<hr>
It took <%="%.3f" %(Time.now-@start_time)%> seconds to generate this page.

To my surprise, the pages renders like so:

enter image description here

When I view source on the page I get:

<!DOCTYPE html>
<html>
<head>
  <title>Dash</title>
  <link href="/assets/application-all.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/all/pages.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/pages.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="SydEiDhSNHuEE6vCfr4rajIksxBbqnm89sddC08msjs=" name="csrf-token" />
</head>
<body>

<h1>Overview</h1>
<hr>
Here's my code test:

&lt;table class=&quot;CodeRay&quot;&gt;&lt;tr&gt;
  &lt;td class=&quot;line-numbers&quot; title=&quot;double click to toggle&quot; ondblclick=&quot;with (this.firstChild.style) { display = (display == '') ? 'none' : '' }&quot;&gt;&lt;pre&gt;

&lt;/pre&gt;&lt;/td&gt;
  &lt;td class=&quot;code&quot;&gt;&lt;pre&gt;puts &lt;span style=&quot;background-color:hsla(0,100%,50%,0.05)&quot;&gt;&lt;span style=&quot;color:#710&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D20&quot;&gt;Hello, world!&lt;/span&gt;&lt;span style=&quot;color:#710&quot;&gt;'&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;

&lt;/tr&gt;&lt;/table&gt;


<hr>
Back <a href="home">home</a>.
<hr>
It took 0.006 seconds to generate this page.




</body>
</html>

Why is the bracketed css displaying as inline text? What should my usage of coderay look like here?

Many thanks -

Perry Horwich
  • 2,798
  • 3
  • 23
  • 51
  • 1
    possible duplicate of [Disable HTML escaping in erb templates](http://stackoverflow.com/questions/4699497/disable-html-escaping-in-erb-templates) – the Tin Man Feb 13 '12 at 04:34
  • 1
    +1 Agreed. My question doesn't add much, except the keyword 'coderay' Might get some other poor soul like me to the answer. I would never have found the similar thread you identify 'cause I just didn't know "escaped HTML" was the problem. – Perry Horwich Feb 13 '12 at 05:47

1 Answers1

1

Rails escapes your HTML by default in ERB templates. You need to turn off HTML escaping like so:

<%=raw CodeRay.scan("puts 'Hello, world!'", :ruby).div(:line_numbers => :table) %>

See more at this question and these release notes.

Community
  • 1
  • 1
Marc W
  • 19,083
  • 4
  • 59
  • 71
  • Yes. Thank you. While I do appreciate all that rails does for me, it's when I don't fully appreciate all that it's doing that I seem to get into trouble. Thanks again. – Perry Horwich Feb 13 '12 at 05:43