Block Comments in HTML.Erb Templates in Rails

How to comment lines in rails html.erb files?

ruby on rails notes has a very nice blogpost about commenting in erb-files

the short version is

to comment a single line use

<%# commented line %>

to comment a whole block use a if false to surrond your code like this

<% if false %>
code to comment
<% end %>

Block comments in html.erb templates in rails

I wouldn't count as a solution, but perhaps enclosing the chunk between an

<% if false %>
...
<% end %>

or if you feel a little dirty, create a helper that simply outputs nothing.

I've never needed it, but I'm stumbled there seems to be no out-of-the-box solution for this.

Best way to add comments in erb

Use the <%# %> sequence, e.g.

<%# This is a great comment! %>

How does one comment in an erb template?

=begin and =end are the Ruby version of block comments.

Using them in an erb template:

<%
=begin
%>
<li class="someclass">
<=% t'model.attr' %>
</li>
<%
=end
%>

How do I comment out ERB in Rails?

<%#= link_to "Make default", make_default_admin_state_path(state) %>

<%# %> is an ERB comment: the dangling = makes no difference, and can be left in.

Why does commented-out ERB still throw errors?

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.

Dealing with quotes in html.erb

There's a helper method called j or escape_javascript that will escape quotes in a string and make it possible to add a string with quotes to an attribute on an element like you're trying to do. More info here

So, change your code to:

<img src="<%=image%>" data-description="<%=j auto_link(step.description)%>"/>

Just adding that j will do it for any sort of string with quotes.

If you're also putting HTML inside an HTML attribute you will have to escape html too with the html_escape helper:

<img src="<%=image%>" data-description="<%=h j(auto_link(step.description))%>"/>

h is short for html_escape. That should escape the tags inside the attribute and not break your layout.



Related Topics



Leave a reply



Submit