I am using a click function to first get the array/position, then post it via an AJAX call to a controller method. I would then like the method to return a boolean value to enable the addClass in the jquery function. The console is consistently throwing a 500 server error.
Here is the .js file:
$(document).ready(function(){
$("#backboard").on("click", "div", function(e){
$("#backboard div").removeClass("selected");
var val = $(this).data('val');
$.ajax ({
type: 'POST',
url: "https://localhost:3000/chess/:pos",
data: {pos: {value: val}},
success: function(d){
if(d === true){
$(this).addClass("selected").append("<p>"+val+"<p>");
}
}
});
});
});
here is the controller.rb file:
class ChessController < ApplicationController
include ChessHelper
def new
@select = false
@game = Game.new("white", "ARSEFACE!!!")
@arr = @game.board
@cpu = @game.cpuName
@player = @game.playerName
end
def select
pos = params[:pos]
a = pos[0]
b = pos[1]
if(@arr[a][b][0]===@color)
@select = !@select
end
respond_to do |format|
format.json { render json: @select }
end
end
end
Here is the config/routes.rb:
get '/chess', to: 'chess#new'
post '/chess/:pos' => 'chess#select'
I know I am probably missing something incredibly obvious here, but any help would be appreciated!
PS and the val variable taking the data from the tag does actually have a value. I am trying to pass that value to the controller in order to return true or false, and will eventually be a validation function once I get this basic issue worked out.