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
How to Install Therubyracer Gem on 10.10 Yosemite
Rails Paperclip How to Delete Attachment
Getting a "Bad Interpreter" Error When Using Brew
Extract a Substring from a String in Ruby Using a Regular Expression
How to Get Rid of Non-Ascii Characters in Ruby
Confusion With Atomic Grouping - How It Differs from the Grouping in Regular Expression of Ruby
Converting String from Snake_Case to Camelcase in Ruby
When to Use Each Method of Launching a Subprocess in Ruby
Nomethoderror When Trying to Invoke Helper Method from Rails Controller
Class ≪≪ Self VS Self.Method With Ruby: What's Better
When Do I Need to Restart Server in Rails
Validation Failed: Upload File Has an Extension That Does Not Match Its Contents
Database.Yml &References Not Working
Extract Number from String in Ruby
Best Practices for New Rails Deployments on Linux
Ruby - Access Multidimensional Hash and Avoid Access Nil Object
Ruby on Rails Callback, What Is Difference Between :Before_Save and :Before_Create