Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, March 1, 2016

Block comments in html.erb templates in rails

Block comments in html.erb templates in rails


How do you comment out html mixed with ruby code?

some text <% ... %> more text <%= ... %>  something else  <% ... %>  

In jsp it's real simple: <%-- ... --%>, but I'm unable to find any concise option in rails.

Simple html comments do not work: ruby code is still executed and yells errors.

There's an option to use if false with html comments, but it's quite verbose, not to mention IDEs doesn't support it.

There's also an option coming from pure ruby, which surprisingly works.

<%  =begin %>  ... html and ruby code goes here  <%  =end %>  

It's generally fine, except that it's verbose, weird-looking and none of ruby IDEs I know support it (yep, I like to comment/comment-out with one keystroke).

I'm curious, is there any 'official' of doing this in rails?

Thanks!

Answer by John Topley for Block comments in html.erb templates in rails


You have to bear in mind where the code is executed. Ruby-style comments work because the Ruby code is executed on the server before it is served to the web browser. This also explains why HTML comments do not work?the Ruby has already been executed.

Doesn't the IDE you're using support creating custom macros for commenting out blocks of code?

Answer by Garfield for Block comments in html.erb templates in rails


Use this for commenting single lines:

<%# your_ruby_code %>  

For multiple lines, the

<%   =begin %>  <% ruby_code %>  <%   =end %>  

What you said would work.

Answer by Chubas for 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.

Answer by jamesw for Block comments in html.erb templates in rails


To comment out erb tags use the ruby comment hash symbol before the = sign in the opening tag

This is some text I want to keep <%= @some_object.some_attribute %>

I want to keep this text but comment out the erb tag <%#= @some_object.another_attribute %>

Answer by iono for Block comments in html.erb templates in rails


Sublime Text's block comment shortcut ctrl+shift+/ notices whether you've selected normal HTML or an Erb tag and puts either the

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.