Ruby on Rails Gon Gem vs. cookie or regular AJAX request

I programmed Ruby on Rails for a while, and in order to be able to easily receive data from my controllers in my javascript code, as a newbie, I came across Gon Gem . This solves the problem quite simply, but a little mysteriously for RoR beginners, to be honest.

Diving into the source code of the gem, now I understand how it works: The variables that you request gonto go to your JS are tracked by the stone and inserted into the tag <script />in your view displayed by the tagerb

<%= Gon::Base.render_data %>

which makes the object gonaccessible through your JS as it introduces it as a property window.

My question is: how good is the practice? Honestly, it seems a bit suspicious to dynamically inject JS code into the site in order to access some data from the server in the browser. Isn't that the best practice for

  • just make data accessible through an endpoint on the RoR server side and an AJAX call from the JS interface?

or

  • to set the necessary data in a cookie during the corresponding action on the RoR controller and just read it from JS?
+4
source share

Source: https://habr.com/ru/post/1670960/


All Articles