![](/img/trans.png)
[英]how do i pass location coordinate getting from HTML5 & javascript to my rails controller and mongodb model ?
[英]How to use AJAX request to pass user's HTML5 location in a class variable from JavaScript to Rails controller?
我在 Rails (v6.0.3.4) 和 Geokit-rails (2.3.2) 上使用 Ruby (2.7.2p137)、Ruby。
我正在嘗試將用戶的位置傳遞給 Geokit-rails gem 中的方法。 為此,我正在嘗試使用 AJAX 請求將用戶的 HTML5 位置從 JavaScript 傳遞到 Rails ZE4904C103F2C03E1。
更新:執行此操作需要 Class 變量而不是實例變量。 答案中對此進行了解釋。
以下是調用 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:
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 controller (properties_controller.rb) 中的方法,它根據指定坐標按距離顯示屬性列表:
def show_location
@user_lat = user_lat
@user_long = user_long
@properties = Property.by_distance(:origin => [@user_lat, @user_long])
end
以下是我的屬性 controller 中用於 AJAX 請求的方法:
def user_lat
@user_lat = params["lat"]
end
def user_long
@user_long = params["long"]
end
我知道 gem 和 controller 正在工作,因為當我將坐標硬編碼到 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 中的方法將用戶的緯度和經度存儲到實例變量而不是 class 變量。
實例變量不能在方法之外使用/訪問,而 class 變量可以在 class 內的任何方法中使用/訪問。
這在 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
然后將 class 變量傳遞給 show_location 方法。
def show_location
@properties = Property.by_distance(:origin => [@@user_lat, @@user_long])
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.