开发者

Rails 3 - How to comment in a View?

开发者 https://www.devze.com 2023-01-16 21:38 出处:网络
What is the Rails 3 way to comment out One Line o开发者_如何学JAVAr Multiple lines of code in a View? And so it doesn\'t show up in the HTML SourceTo comment out a single line ob ruby code use

What is the Rails 3 way to comment out One Line o开发者_如何学JAVAr Multiple lines of code in a View? And so it doesn't show up in the HTML Source


To comment out a single line ob ruby code use

<%# code %>
or for multiple lines
<%
=begin
 your code
=end
%>

EDIT: Here a example to comment out a loop in an view. The =begin and =end must stand directly at the beginning of the line. There couldn't be any spaces or tabs.

<h1>Listing posts</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<%
=begin 
%>
<%@posts.each do |post| %>
  <tr>
    <td><%= post.title %></td>
    <td><%= post.text %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
<%
=end
%>
</table>


Rails 3 line comment within a view:

The f.label :name line has been commented out:

<%= form_for(@usr) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%#= f.label :name %>
  <%= f.text_field :name %>

  <%= f.submit "Save changes" %>
<% end %>

Because this is a view, the # must be within <% and %>.


Rails 3 multi-line comment within a view:

Begin multi-line comment:

<%
=begin
%>


End multi-line comment:

<%
=end
%>


Below, the entire form_for block has been commented out:

<%
=begin
%>

<%= form_for(@usr) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.submit "Save changes" %>
<% end %>

<%
=end
%>


Do take note, for the multi-line comment tags to work, there can be no spaces or tabs in front of =begin or =end. They need to be at the very beginning of a line, or they will not work.


Here's what I do to hide comments from HTML (...whatever, it works!)

in your helpers/application.rb file:

def comment
    # use this keyword in the views, to comment-out stuff...
end

in your view:

<h3><% comment = "my Rails-y something something here will not be visible to HTML.
    I don't know why I have to comment stuff out in this manner.
    It's probably not the 'Rails Way' but ...it works for me.
    It would be nice if Rails 3 had some kind of comment-out feature like <%## %> or <%-- --%>  or something like that...
    Ah, well... 
    At least they won't be able to 'View Source' and read this comment! ;]
" %></h3>

what 'View Source' shows:

<h3></h3>

C-YA!


Some ways to comment code

<%
=begin
%>

RUBY CODE GOES HERE

<%
=end
%>

<% if false %>

RUBY CODE GOES HERE

<% end %>

<%# RUBY CODE%>
<%#= RUBY CODE%>

<!-- 
HTML CODE
-->

For RUBY code in.rb files like models/controllers

def xyz
  =begin
  blah blah code
  =end
end

For JS etc

/*
Some code
*/


which "blocks" do you mean? html? then you can use ruby code? <%# code %>

0

精彩评论

暂无评论...
验证码 换一张
取 消