AntonAL
AntonAL

Reputation: 17410

Avoid rendering comments in HTML

I have a lot of comments in Rails views.

How i can prevent rendering them ?

Upvotes: 6

Views: 1192

Answers (6)

harrymasson
harrymasson

Reputation: 176

If I understand the question correctly, you're asking about Ruby/Rails comments vs HTML comments... Give this a try in your view:

<!-- This is an HTML comment and will show up in the HTML source! -->

Now try this:

<%# This is a comment that won't show up in the HTML source! %>
<%# 
    You can even use this for commenting multiple lines!
    How useful!
%>

Does that help?

Upvotes: 7

kalyanji
kalyanji

Reputation: 75

Maybe you can use Haml Comments: -# allow to comment your haml code without them appearing in the generated html.

Upvotes: 1

Dan McNevin
Dan McNevin

Reputation: 22336

Kind of hackish, but you can wrap it in a helper method

In your view:

<% comment do %>
  <%= "this won't be executed" %>
  or displayed
<% end %>

in app/helpers/application_helper.rb

module ApplicationHelper
  def comment(&block)
    ## you can do something with the block if you want
  end
end

Upvotes: 1

Liam
Liam

Reputation: 1768

I'm not a Rails programmer, but a quick but of Binging brought up this link: http://blog.brijeshshah.com/strip-tags-in-rails-javascript-and-php/

The approach he's using is one that I've used in the past where you sanitize the view's output. sanitize being the name of the function you want to use before rendering the view.

Upvotes: 1

Daniel O&#39;Hara
Daniel O&#39;Hara

Reputation: 13438

There is no easy way to do that. You can monkey patch ERB sources, perhaps, but it is a bit nerdy.

Upvotes: 1

Nealv
Nealv

Reputation: 6884

use =begin and =end to mark the beginning and end of your comment

Upvotes: 2

Related Questions