如何将params传递给Rails控制器方法然后使用JQuery / AJAX返回值? [英] How do I pass params to Rails controller method then return value using JQuery/AJAX?
问题描述
我使用click函数首先获取数组/位置,然后通过AJAX调用将其发布到控制器方法。然后我想让方法返回一个布尔值来启用jquery函数中的addClass。控制台一直抛出500服务器错误。
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.
这是.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>");
}
}
});
});
});
这里是controller.rb文件:
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
这是config / routes.rb:
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和从变量获取数据的val变量实际上有一个值。我试图将该值传递给控制器以返回true或false,并且一旦我解决了这个基本问题,最终将成为验证函数。
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.
推荐答案
url:https:// localhost:3000 / chess /:pos,
将不会被插值,除非我完全遗失了一些东西。
url: "https://localhost:3000/chess/:pos",
won't be interpolated, unless I'm completely missing something.
使用发布'/ chess /:pos'=> 'chess #select'
你告诉Rails(特别是它的路由器)在位置的请求路径中寻找一个参数:pos
,当它找到它时,将 params
哈希的值(在 params [:pos]
)设置为路径中: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.
jQuery对Rails的路由器一无所知。也不是Javascript。
jQuery doesn't know anything about Rails' router. Neither does Javascript.
要明白我的意思,请将其添加到 routes.rb
文件中:
To see what I mean, add this to your routes.rb
file:
get'/:cookies / and /:cream'=> 'application #test'
在 application_controller.rb
中,添加以下内容: / p>
in application_controller.rb
, add the following:
def test
render :text => "I love me some #{params[:cookies]} with my #{params[:cream]}"
end
然后点击 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>");
}
}
});
应该这样做。无需在ajax函数中传递任何额外的数据
。
should do it. No need to pass any additional data
in the ajax function.
另外,不要在开发中使用HTTPS。这只会让你头疼。
Also, don't use HTTPS in development. It'll just cause you headaches.
这篇关于如何将params传递给Rails控制器方法然后使用JQuery / AJAX返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!