Right now, I have some tooltip JavaScript that I’d like to run on every page of my app, but only if the current user’s model has User.advanced_tooltips set to true in my database.
What is the best way conceptually to get this JavaScript to trigger conditionally based on a value in a database? I’m having some real trouble getting JavaScript and Rails to communicate.
Here’s the solution I ended up using (given the very weird constraints imposed by this project):
First, I set up a controller route that finds the current User and then renders its advanced_tooltip value as text.
Then, in my application.js, I made an AJAX call using $.get to that route, then checked whether the result == “true” to determine whether or not a user had advanced tooltips enabled.
It’s a messy solution (and those listed above are actually better), but since I had some very rigid restrictions due to me jumping onto someone else’s project’s mid-completion, it’s what I had to do.
Still gave both comment up-votes, as they’re valid and would totally work under normal circumstances.