Commenting in an ERB without generating a compiling error

Hello,

I have the following:

<% @projects.each do |project| %>
  <tr>
.
  <td><%= link_to 'Destroy', project, :confirm => 'Are you
sure?', :method => :delete %></td>
.
  </tr>
<% end %>

How can I safely comment out the TD line above? Do I have to use the
=begin way?

<% @projects.each do |project| %>
<tr>
.
  <td><%= link_to 'Destroy', project, :confirm => 'Are you
sure?', :method => :delete %></td>
.
</tr>
<% end %>

How can I safely comment out the TD line above? Do I have to use the
=begin way?

If you don't mind HTML comments, just wrap it in <!-- ..... ->

If you just want the link gone, but keep the <td> you can make the start tag look like <%#= and it should work (if memory serves).

Thanks for the reply. I need to avoid HTML comments. Also, I tried

<%= <td><%= link_to 'Destroy', project, :confirm => 'Are you >
sure?', :method => :delete %></td> %>

And what happens is every time it runs through the loop it outputs on
the page: </td> %>, meaning the " %> from the TD is being used to
close the comment, which is why I'm trying to find out a way to
comment this line out

Thanks

because the parser see this
<%#=

<%= link_to ‘Destroy’, project, :confirm => ‘Are you >
sure?’, :method => :delete %>

and thinks i ended there
so it thinks

%> is text

put a <%#= in front of the %>

Thanks for the reply. I need to avoid HTML comments. Also, I tried

<%= <td><%= link_to 'Destroy', project, :confirm => 'Are you >
sure?', :method => :delete %></td> %>

And what happens is every time it runs through the loop it outputs on
the page: </td> %>, meaning the " %> from the TD is being used to
close the comment, which is why I'm trying to find out a way to
comment this line out

This is evil, but it will work...

<%%q{ <td><%= link_to 'Destroy', project, :confirm => 'Are you >
sure?', :method => :delete %></td> }%>

The %q{...} turns everything within it into an un-evaluated string. And the <% without the <%= results in no output. If you don't even want the blank line change the last %> to -%>

-philip

Thanks all, just a little surprised that Rails doesn't have a simpilar
way to comment things out in the view.

nobosh wrote:

Thanks all, just a little surprised that Rails

You mean ERb.

doesn't have a simpilar
way to comment things out in the view.

Sure it does:
<%# comment %>

You should probably be using Haml instead of ERb, though.

Best,

nobosh wrote:

doesn't have a simpilar
way to comment things out in the view.

Sure it does:
<%# comment %>

That won't help him comment his <td> though...

You should probably be using Haml instead of ERb, though.

Why? What failure is the OP experiencing that HAML would make succeed?
(BTW, I like HAML, but it's this sort of exclamation of yours without
any supporting citations that seems to rile people - try rephrasing
along the lines of "check out HAML for a more succinct and easier to
read way of rendering your views rather than using the bundled Erb"...
or something). If there's a method in HAML that would allow him to
comment his <td> without a lot of fussing, show that too, since that's
the problem he's facing.

Back to the OP:

I use a fairly straightforward way of removing code from execution, in
the view, model, controller, and in whatever language I happen to be
using - wrap it in a conditional check that doesn't return true:

<% if false %>
       <td><%= link_to 'Destroy', project, :confirm => 'Are you
sure?', :method => :delete %></td>
<% end %>

Similarly, if you *never* want an existing conditional statement to pass, use:
<% if false && <your original condition here> %>

and if you *always* want it to pass:
<% if true || <your original condition here> %>

...easy to add and easy to remove.

Michael Pavling wrote:

nobosh wrote:

doesn't have a simpilar
way to comment things out in the view.

Sure it does:
<%# comment %>

That won't help him comment his <td> though...

Of course it will. He can just put the whole <td> construct in an ERb
comment tag. ERb doesn't pay attention to HTML nesting. Or am I
misunderstanding what you're saying?

Or is this an issue of multiline comments in ERb? I font think it is,
but if so, then =begin or if false may be necessary.

You should probably be using Haml instead of ERb, though.

Why? What failure is the OP experiencing that HAML would make succeed?

Haml does have a nicer comment syntax, but I suggested it because it's
generally more pleasant to work with than ERb, not due to a "failure".

(BTW, I like HAML, but it's this sort of exclamation of yours without
any supporting citations that seems to rile people - try rephrasing
along the lines of "check out HAML for a more succinct and easier to
read way of rendering your views rather than using the bundled Erb"...
or something).

I and others have probably said that a hundred times in this list by
now. That's why we have archives...

Also, I didn't want to hijack the thread.

If there's a method in HAML that would allow him to
comment his <td> without a lot of fussing, show that too, since that's
the problem he's facing.

Now that would *really* be hijacking the thread.

Back to the OP:

I use a fairly straightforward way of removing code from execution, in
the view, model, controller, and in whatever language I happen to be
using - wrap it in a conditional check that doesn't return true:

<% if false %>
       <td><%= link_to 'Destroy', project, :confirm => 'Are you
sure?', :method => :delete %></td>
<% end %>

Yes, that's a very useful trick.

Similarly, if you *never* want an existing conditional statement to
pass, use:
<% if false && <your original condition here> %>

and if you *always* want it to pass:
<% if true || <your original condition here> %>

...easy to add and easy to remove.

Cool!

Best,

The original line was:
  <td><%= link_to 'Destroy', project, :confirm => 'Are you sure?',
:method => :delete %></td>

so If I changed that to :

<%# <td><%= link_to 'Destroy', project, :confirm => 'Are you sure?',
:method => :delete %></td> %>

I would be squawked at by the parser, or rather, just get an
extraneous "</td> %>" in my HTML, because the comment would be closed
by the first "%>"

Which is why I just tend to cut the the chase, and put an "if false"
around the lot.

@Michael Pavling

wow thats a good one

Michael Pavling wrote: