title: How to pass model data to JavaScript (Rails)
body: <p>I have an MVC architecture I would like to maintain. In my view, I have a button that will cause the chessboard to be updated according to some row in the ChessPosition database.
  Currently, I am inputting the data (chess position) into a hidden span and then accessing the span in JavaScript. Obviously, this is jank. I would also have trouble updating the span for the next position if the button is clicked again.
  What's a good way of passing the model into JavaScript? In my controller, I load the model into a variable, can I use that in JavaScript? If so, how?</p>
  <p>HTML:</p>
  <pre><code>&lt;span id=&quot;tactic_info&quot; style=&quot;visibility: hidden&quot;&gt;&lt;%=generate_tactic(@tactic_model) %&gt;&lt;/span&gt;
  &lt;button id=&quot;tactic_generator&quot;&gt;Generate Tactic&lt;/button&gt;
  </code></pre>
  <p>helper:</p>
  <pre><code>module AppsHelper
    def generate_tactic(model)
      id = Random.new.rand(1..Tactic.all.size)
      t = model.find(id)
      @tactic = [t.fen, t.solution]
    end
  end
  </code></pre>
  <p>JS:</p>
  <pre><code>let tactic, tg;
  window.onload = function() {
      tactic = document.getElementById(&quot;tactic_info&quot;).textContent;
      tg = document.getElementById(&quot;tactic_generator&quot;);
      tg.onclick = function c() {
          console.log(tactic.split(',', 2));
      };
  };
  </code></pre>