Search code examples
ruby-on-railsrubyruby-on-rails-4herokuproduction-environment

Is it possible to publish a site without its comments in production?


I am only just starting to program and in my code I have a lot of silly comments (<!-- comment -->) that are really only meant for myself. For example lots of links to stackoverflow articles to understand my own code as well as explanations to myself why I wrote the code the way I did.

This code is included in views (html) as well as server-side code (e.g., controller and model files).

For a visitor of my website these comments might look silly and amateuristic. So if they'd visit my website and looked at the source code I would like to not have those comments included.

Is there a way to publish a site without its comments in production? (while keeping the comments in development)

I am using Rails4 and Heroku.

Update: I understand now that comments in model and controller files won't be visible to visitors of my web page, anyway. So then it only concerns the comments in my view pages (.html.erb), i.e., embedded html comments in your webpages. It could also concern Javascript, but currently my website hardly contains any of that. Does anyone have an example of a script for removing these comments when pushing to production/heroku?


Solution

  • If you're trying to omit html comments from the source code, you can convert your html comments to erb comments as erb comments wont get rendered to the markup

    so convert your html comments:

    <!-- comment -->
    

    to erb comments:

    <%# comment %>
    

    its a little hacky, but it should work for you. this will keep the comments in your code base viewable from the editor or github, but will omit them from being visible from the client (in any environment, not just production)