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.
我使用click函数首先获取数组/位置,然后通过AJAX调用将其发布到控制器方法。然后我想让方法返回一个布尔值来启用jquery函数中的addClass。控制台始终丢失500服务器错误。
Here is the .js file:
这是.js文件:
$(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:
这是controller.rb文件:
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:
这是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.
PS和从变量中获取数据的val变量确实有一个值。我试图将该值传递给控制器以返回true或false,并且一旦我解决了这个基本问题,最终将成为验证函数。
1 个解决方案
#1
2
url: "https://localhost:3000/chess/:pos",
won't be interpolated, unless I'm completely missing something.
url:“https:// localhost:3000 / chess /:pos”,不会被插值,除非我完全遗漏了什么。
With post '/chess/:pos' => 'chess#select'
you're telling the Rails (its router specifically) to look for a param inside the request path in the position of :pos
, and when it finds it, set the value of the params
hash (at params[:pos]
) to the string at :pos
in the path.
使用post'/ chess /:pos'=>'chess #select'你告诉Rails(特别是它的路由器)在位置:pos中查找请求路径中的param,当它找到它时,设置params散列(在params [:pos]处)到路径中的pos:pos的值。
jQuery doesn't know anything about Rails' router. Neither does Javascript.
jQuery对Rails的路由器一无所知。也没有Javascript。
To see what I mean, add this to your routes.rb
file:
要查看我的意思,请将其添加到routes.rb文件中:
get '/:cookies/and/:cream' => 'application#test'
得到'/:cookies /和/:cream'=>'application#test'
in application_controller.rb
, add the following:
在application_controller.rb中,添加以下内容:
def test
render :text => "I love me some #{params[:cookies]} with my #{params[:cream]}"
end
then hit http://localhost:3000/chocolate-chip/and/milk
然后点击http:// localhost:3000 / chocolate-chip /和/ milk
should read "I love me some chocolate-chip with my milk"
应该读“我爱我一些巧克力片加牛奶”
Now, assuming your other code is fine,
现在,假设您的其他代码没问题,
$("#backboard").on("click", "div", function(e){
$("#backboard div").removeClass("selected");
var val = $(this).data('val');
$.ajax ({
type: 'POST',
url: "http://localhost:3000/chess/" + val,
success: function(d){
if(d === true){
$(this).addClass("selected").append("<p>"+val+"<p>");
}
}
});
should do it. No need to pass any additional data
in the ajax function.
应该这样做。无需在ajax函数中传递任何其他数据。
Also, don't use HTTPS in development. It'll just cause you headaches.
另外,不要在开发中使用HTTPS。这只会让你头疼。
#1
2
url: "https://localhost:3000/chess/:pos",
won't be interpolated, unless I'm completely missing something.
url:“https:// localhost:3000 / chess /:pos”,不会被插值,除非我完全遗漏了什么。
With post '/chess/:pos' => 'chess#select'
you're telling the Rails (its router specifically) to look for a param inside the request path in the position of :pos
, and when it finds it, set the value of the params
hash (at params[:pos]
) to the string at :pos
in the path.
使用post'/ chess /:pos'=>'chess #select'你告诉Rails(特别是它的路由器)在位置:pos中查找请求路径中的param,当它找到它时,设置params散列(在params [:pos]处)到路径中的pos:pos的值。
jQuery doesn't know anything about Rails' router. Neither does Javascript.
jQuery对Rails的路由器一无所知。也没有Javascript。
To see what I mean, add this to your routes.rb
file:
要查看我的意思,请将其添加到routes.rb文件中:
get '/:cookies/and/:cream' => 'application#test'
得到'/:cookies /和/:cream'=>'application#test'
in application_controller.rb
, add the following:
在application_controller.rb中,添加以下内容:
def test
render :text => "I love me some #{params[:cookies]} with my #{params[:cream]}"
end
then hit http://localhost:3000/chocolate-chip/and/milk
然后点击http:// localhost:3000 / chocolate-chip /和/ milk
should read "I love me some chocolate-chip with my milk"
应该读“我爱我一些巧克力片加牛奶”
Now, assuming your other code is fine,
现在,假设您的其他代码没问题,
$("#backboard").on("click", "div", function(e){
$("#backboard div").removeClass("selected");
var val = $(this).data('val');
$.ajax ({
type: 'POST',
url: "http://localhost:3000/chess/" + val,
success: function(d){
if(d === true){
$(this).addClass("selected").append("<p>"+val+"<p>");
}
}
});
should do it. No need to pass any additional data
in the ajax function.
应该这样做。无需在ajax函数中传递任何其他数据。
Also, don't use HTTPS in development. It'll just cause you headaches.
另外,不要在开发中使用HTTPS。这只会让你头疼。