Using a Rails Constant in a Javascript Alert Popup

A little ditty for you if you want to display a Rails Constant in a Javascript Alert Popup.  Mostly screenshots for you on how I did it without writing a lot of code.

Let’s start with the endgame. This is what we want to see. “This feature is on the to-do list and not yet available” is our constant.

Our constant is defined in an Application Helper module.

What Doesn’t Work

Here are a few approaches that you might think would work, but do not.

Our <%= coming_soon %> isn’t going to do the job, either in single or double quotes.

You might think a little interpolation will do the trick, but that won’t exactly produce our desired result either.

What Works

Hey, what about using a Rails confirmation box?  That’s the ticket! Add some string interpolation and we’ve got a constant displaying in a javascript alert popup without a lot of fancy code.

This is the resulting popup shown at the top of the post. We’ve got a “Cancel” button that we could probably do without, but sometimes efficiency rules the day.