ajax + rails send ajax data to controller

-2

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)

    
asked by ninithblue 04.09.2018 в 14:50
source

1 answer

0

Your code does not work correctly because you are sending your parameters without grouping (i.e. data: { puntos: 4 } , but your controller expects a grouping (i.e. params.require(:game).permit(:puntos) ).

You can change the script or the driver; I lean more for the first option, which would look like this:

$.ajax({
  type: "POST",
  url: "<%= games_create_path %>",
  dataType:'json',
  data: {
    game: { puntos: 4 }
  },
  success: function(msg) {
    console.log("conseguido");
    alert("conseguido");
  },
  error: function(msg) {
    console.log("Sorry...");
    console.log(msg);
    console.log(msg.responseText);
  }
});

Also you are saving twice @game in your controller, but once you do it is enough:

def create
  @game=Game.new(task_params)
  @game.save

  respond_to do |format|
    format.js
  end
end

Regarding the error:

  

jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js? body = 1: 10255   POST localhost: 3000 /% 3C% = games_create_path %% 3E 404 (Not Found)

The problem is that the file where you have the javascript code is termination .js and should be .js.erb so that the code <%= games_create_path %> is replaced correctly by the appropriate URL.

Finally, you have an error in your routes, you must use to: to indicate which action the route points to:

Rails.application.routes.draw do
  post 'games/create', to: 'games#create'
  get  'games/show'

  root 'games#index'
end

BONUS

You could update your routes using resources since you are not using custom actions; In addition, it is best practice that root always go to the top of your routes:

Rails.application.routes.draw do
  root 'games#index'

  resources :games, only: [:show, :create]
end

Consider that with the previous code, your route can now be obtained through helper games_path , so you must update your code:

url: "<%= games_path %>",
    
answered by 04.09.2018 в 16:09