如何使用 AJAX 请求将用户在类变量中的 HTML5 位置从 JavaScript 传递到 Rails 控制器?



我使用Ruby (2.7.2p137), Ruby on Rails (v6.0.3.4)和Geokit-rails(2.3.2)。

我试图将用户的位置传递到Geokit-rails gem的方法中。为此,我试图使用AJAX请求将用户的HTML5位置从JavaScript传递到Rails控制器。

更新:这需要类变量而不是实例变量来完成。这在答案中有解释。

下面是服务器调用show_location方法时的错误/响应:

Started GET "/show_location?user_location%5B%7B%3Avalue%3D%3Enil%7D%5D=" for ::1 at 2021-02-21 13:03:18 +0000
Processing by PropertiesController#show_location as HTML
Parameters: {"user_location"=>{"{:value=>nil}"=>""}}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms | Allocations: 1235)  

NoMethodError (undefined method `*' for nil:NilClass):
app/controllers/properties_controller.rb:77:in `show_location'

服务器上显示了以下响应,因此index.js中的JavaScript似乎正在工作:

Started POST "/user_long" for ::1 at 2021-02-21 12:57:13 +0000
Started POST "/user_lat" for ::1 at 2021-02-21 12:57:13 +0000
Processing by PropertiesController#user_lat as */*
Parameters: {"lat"=>"53.3200896"}
No template found for PropertiesController#user_lat, rendering head :no_content
Completed 204 No Content in 80ms (ActiveRecord: 0.0ms | Allocations: 2885)

Processing by PropertiesController#user_long as */*
Parameters: {"long"=>"-6.2521344"}
No template found for PropertiesController#user_long, rendering head :no_content
Completed 204 No Content in 34ms (ActiveRecord: 0.0ms | Allocations: 2340)

这是在浏览器中记录到控制台的HTML5位置对象:

Object
latitude: 53.3200896
longitude: -6.2521344
__proto__: Object

下面是我的index.js文件中请求HTML5位置的JavaScript:

//function that gets the location and returns it
function getLocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
console.log("Geo Location not supported by browser");
}
}
//function that retrieves the position
function showPosition(position) {
var location = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
}
$(document).ready(function() {
var lat = $(this).val();
$.ajax({
type: 'post',
url: '/user_lat',
data: {lat: position.coords.latitude},
success: function(data) {
console.log(data);
}
});      
})
$(document).ready(function() {
var long = $(this).val();
$.ajax({
type: 'post',
url: '/user_long',
data: {long: position.coords.longitude},
success: function(data) {
console.log(data);
}
});      
})
console.log(location)
}
//request for location
getLocation();

这是我的Rails控制器(properties_controller.rb)中的方法,它根据指定的坐标按距离显示属性列表:

def show_location
@user_lat = user_lat
@user_long = user_long
@properties = Property.by_distance(:origin => [@user_lat, @user_long])
end
以下是我的属性控制器中用于AJAX请求的方法:
def user_lat
@user_lat = params["lat"]
end
def user_long
@user_long = params["long"]
end

我知道gem和控制器正在工作,因为当我硬编码坐标到show_location方法时,它成功地工作了。工作方法如下。

def show_location

@properties = Property.by_distance(:origin => [53.3200896,-6.2521344])
end

所以我只需要帮助传递用户的位置到这个方法。

使用上述代码,我收到以下错误:
undefined method `*' for nil:NilClass

与行相关:

@properties = Property.by_distance(:origin => [@user_lat, @user_long])

感谢您的帮助。

我明白了。AJAX请求正常工作,但是properties_controller中的方法将用户的经度和纬度存储到实例变量中,而不是类变量中。

实例变量不能在方法外部使用/访问,而类变量可以在类内的任何方法中使用/访问。

在下面的properties_controller中更正了这一点。rb:

def user_lat
@@user_lat = params["lat"]
session[:user_lat] = @@user_lat
end
def user_long
@@user_long = params["long"]
session[:user_long] = @@user_long
end

然后将类变量传递给show_location方法。

def show_location
@properties = Property.by_distance(:origin => [@@user_lat, @@user_long])
end

最新更新