I'm having trouble incrementing an integer column (plays
) for the tracks
model in a Rails music app. Users can play tracks in the app using buttons like this one:
<% @top_tracks.each do |track| %>
<button class="track-play track-<%= track.id %>-play" onclick="playTrack(<%= track.to_json %>)">
<i class="icon-play"></i>
</button>
...
[info about the track, such as <%= track.title %>]
...
<% end %>
And here are the relevant parts of the corresponding Javascript:
var player = document.getElementById('audio');
window.playTrack = function (track) {
player.setAttribute('src', track.file_url);
player.play();
};
I have read through answers to somewhat similar questions, such as this one, that suggest utilizing Ajax/Javascript, but I'm not sure how to do that to get the desired result in my case. I don't even know if Ajax/Javascript is the best way to achieve what I'm looking for.
What should happen is that every time a track is played and the playTrack()
function is called, the value of the plays
column for that track should be incremented by 1 and then (obviously) the new value for plays
should be saved in the database.