0

I have an HTML file in this format:

html
more html
<%= valid erb %>

<!--
  <%= incomplete_erb_with_bugs %>
-->

But I still get an exception page, because of the buggy ERB. But shouldn't commenting that section out cause that part not to be read by the browser? Is there another HTML method for actually preventing the browser from reading code?

Joe Morano
  • 1,715
  • 10
  • 50
  • 114
  • Possible duplicate of [Block comments in html.erb templates in rails](http://stackoverflow.com/questions/3127644/block-comments-in-html-erb-templates-in-rails) – burnettk Apr 25 '17 at 00:32

3 Answers3

3

Because its not commented.

But shouldn't commenting that section out cause that part not to be read by the browser?

The browser does not execute Ruby or ERB - the server does before it sends the resulting HTML document to the browser.

ERB is ruby code imbedded in a file that contains literal text. The interpreter does not care about anything except the code in "erb tags".

This is just literal text 
<%# this is a ruby line comment - the code below is executed: %>
<% bar do %>
  <%= foo %>
<% end %> 

The rest is just placed in the buffer. This is just like PHP or any other embedded language.

So a HTML comment (or CSS or JS for that matter) does not effect the ERB interpreter in any way. The interpreter does not really know or care that its creating HTML.

Is there another HTML method for actually preventing the browser from reading code?

The browser does not execute Ruby code. It just builds a document from whatever you send in the response.

So use a ruby comment <%#= incomplete_erb_with_bugs %> which will prevent the code from being executed - and it will never get sent to the browser.

max
  • 96,212
  • 14
  • 104
  • 165
  • This is for single line comments. For multi-line comments see http://stackoverflow.com/questions/3127644/block-comments-in-html-erb-templates-in-rails. – max Apr 25 '17 at 03:09
2

you're commenting out html, but the ruby code inside it still gets evaluated. you need to comment out the code, which is done like this:

<%#= incomplete_erb_with_bugs %>
burnettk
  • 13,557
  • 4
  • 51
  • 52
1

you need to comment the actual line, not the html surrounding it

html
more html
<%= valid erb %>


  <%= incomplete_erb_with_bugs %>

you can also use if statements

<% if false %>
      <%#= incomplete_erb_with_bugs %>
<% end %>
Gaston
  • 1,004
  • 1
  • 9
  • 23