SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to comment in rails template and...

    If i have to comment a block of code in a rails .rhtml template that comprises both Erb code and html code how could i do that? If i have to comment out only the html can i use html comment syntax instead?

    Then i don't know if there is a block comment syntax (multiline comment) in ruby like the /* */ in php thank you.

  2. #2
    SitePoint Member dylanjones's Avatar
    Join Date
    Sep 2006
    Location
    Canada
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To comment in rails I think it is;

    <&#37; #I'm a comment! %>

    that would be in the view, and if you want to comment in the controller just put the # I'm a comment.

    I think you would need to put the # in side the ruby, and use the normal html comment to get rid of the html. Try a combination of both.

    I hope that helps!

  3. #3
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dylanjones View Post
    To comment in rails I think it is;

    <% #I'm a comment! %>

    that would be in the view, and if you want to comment in the controller just put the # I'm a comment.

    I think you would need to put the # in side the ruby, and use the normal html comment to get rid of the html. Try a combination of both.

    I hope that helps!
    that is what i guessed about it but i still need a way to comment out a block of ruby code (more than one line at a time) without using a "#" character on every line of code.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    =begin
    =end

  5. #5
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Code:
    =begin
    =end
    wow i saw that somewhere but i didn't believe that it didn't exixst a simpler form like for example /* */ pair in php or CSS.
    So with a block of ruby code to comment out i begin a line with "=begin", then the next line starts the block of code and in the final line i put "=end" all alone?
    Do "=begin" and "=end" stay in the very first column?
    Then...if i had a very large block of code in a rails view template (.rhtml file) compose of Erb and html how can i comment it all out with just one syntax? Is there a solution instead of comment out the html with html comments and Erb with "#" on every line?

  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Why do you need a long block of comments like that in a view? If the code you're commenting on is that complex it probably should be moved into a controller or helper and out of the view.

  7. #7
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia View Post
    Why do you need a long block of comments like that in a view? If the code you're commenting on is that complex it probably should be moved into a controller or helper and out of the view.
    for example when you are debugging and you want to comment out a block of a rails template (ruby erb code with also html ) temporarely.

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use HTML comments then. View code shouldn't "do" anything anyway so the execution of the Ruby code doesn't matter.

  9. #9
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Just use HTML comments then. View code shouldn't "do" anything anyway so the execution of the Ruby code doesn't matter.
    that is what i was saying. In a view (.rhtml file) you can comment out a block of html with html comments and a block of ruby code either with =begin and =end or a "#" at the beggining of each ruby code line, but you cannot comment out a code block intermixed with html and ruby code (erb construct) with one syntax.

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But that doesn't matter ;-).

    If you do this:

    Code:
    <!--
    <h1><&#37;= @user.name %></h1>
    -->
    The Ruby code will be executed, but this is not a problem because it's in the HTML comment. You won't see the the h1. You won't see the name.

  11. #11
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    But that doesn't matter ;-).

    If you do this:

    Code:
    <!--
    <h1><%= @user.name %></h1>
    -->
    The Ruby code will be executed, but this is not a problem because it's in the HTML comment. You won't see the the h1. You won't see the name.
    Yes in that simple case i understand you
    But if you have a little more logic in erb in a template you want for example comment out a ruby loop with html intermixed all in one place. So i now know that the only way is to comment out the html with html comments and ruby code with "#" hashes in erbs blocks.

  12. #12
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <&#37; if false %>
    this isn't showing up
    <% end %>
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •