…Rails Troubleshooting: Views Don’t Render JavaScript Current Year

The Problem

One of the first scripts most front-end developers learn how to use is the JavaScript “Current Year” script, for their copyright notice. Something similar to this:

<script type="text/javascript"> document.write(new Date().getFullYear());</script>

However, this will prevent some Rails views from rendering properly.

How Did I Fix This?

Pretty simply, actually. Why use JavaScript to show the current year when we’re building an app using Ruby? The following Rails code works in exactly the same way and is easier to understand:

<%= Time.current.year %>