我知道你使用ajax:
$.ajax({
url: '/home/index',
data: {params1: "", params2: ""},
success: function(response) {
// code to be executed
}
});
但是什么是数据参数和函数响应。我到处寻找,无法找到rails的确切用法
答案 0 :(得分:0)
首先,这不是特定于rails的,因为其他框架也可以使用ajax,因为它是javascript的一部分。
您传入的data
是可以通过params[:arg1]
在rails控制器中访问的参数。键将是您用于索引params
对象的符号的名称。
function(response)
可以被视为控制器操作返回的有效负载。这是一个例子:
def index
if params[:name].present?
@users = User.where("name LIKE ?", params[:name]) #see symbol name
else
@users = User.all
end
render json: @user #something along these lines
end
然后在你的ajax调用中,你可以这样做:
$.ajax({
url: '/users',
data: {name: "bobby"}, // see key name
success: function(response) { // response here would be the returned users
// code to be executed
}
});