I have the following code:
$(document).ready(function(){
$('#score').on('click',function () {
alert("hola");
var score=4;
console.log("dentro de click");
$.ajax({
type: "POST",
url: "<%=games_create_path%>",
dataType:'json',
data: {puntos: 4},
success: function(msg) {
console.log("conseguido");
alert("conseguido");
},
error: function(msg) {
console.log("Sorry...");
console.log(msg);
console.log(msg.responseText);
}
})
});
});
My idea is that when the user clicks on a link that bears the identifier #score
, ajax communicates with a controller that I have in games_create_path
, and the controller saves it in the base data the number 4 in a field named puntos
v (it's a field that is inside a table in the database.).
Doubt: I do not know if I am communicating well with the controller.
The controller:
class GamesController < ApplicationController
def index
@games = Game.all
end
def new
@game = Game.new
end
def create
@game=Game.new(task_params)
@game.save
respond_to do |format|
format.js
end
@game.save
end
private
def task_params
params.require(:game).permit(:puntos)
end
end
Routes:
Rails.application.routes.draw do
post 'games/create', 'games#create'
get 'games/show'
root 'games#index'
end
Error:
jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js? body = 1: 10255 POST localhost: 3000 /% 3C% = games_create_path %% 3E 404 (Not Found)