I want to make a game type quiz, that according to the correct answers the user earns X points. Those x points I want to save in a table called Games that has a field called points. I would like to do it without having to do any submit. That is, when the game ends, the score is saved. As a practical example I tried some code but I lost a lot. In the following code I have imagined that the score is always 4, and that when the user clicks on a button, the score 4 is saved in the database.
$(document).ready(function(){
$('#score').on('click',function () {
alert("hola");
var score=4;
Rails.ajax({
type: "POST",
url: "/games/create",
dataType:'json',
data: {puntos: score},
success: function(msg) {
alert("adios");
$('#autosavenotify').text("%SDGSFsdfs");
}
})
});
class GamesController < ApplicationController
def index
@games = Game.all
end
def new
@game = Game.new
end
def create
@game=Game.new(task_params)
@game.save
end
private
def task_params
params.require(:game).permit(:puntos)
end
end