I've delved into a plethora of information regarding Rails, AJAX, and 5.1 Unobtrusive Javascript. It provides insight on how to handle AJAX calls in Rails with a .js file, for example.
However, my goal isn't to serve up an entire .js file; rather, I aim to update an element after a <% link_to %>
POST request. As far as I understand, setting remote: true
submits it as an AJAX request.
Essentially, I have a "Post" that users can like by clicking a Like button. This triggers a POST request to the "Post" controller, which then updates the post as liked and adds a like to it.
Unfortunately, to witness the effects of liking the post (which simply changes the color of the link and the font-awesome icon), one must refresh the page. My aim is to have these updates occur without requiring a page refresh.
After some research, I believe I need to use respond do
and respond via a .js file to properly handle the request with a given view to be updated (for instance, if the controller action is named "like", maybe a like.js.erb
file would be used for updating?). However, I wish to avoid serving an entirely new page or only run the .js file?
Subsequently, I could potentially utilize something like
$('i.fa-icon#id').style.color = "blue"
or similar? (I assume data can be transmitted from the controller to the .js.erb file?). I am uncertain about the best approach for this, as Rails elements often feature some sort of data-attribute
or something akin to that (given my status as a beginner).