我将如何为使用 html5 地图的用户在我的 postgres table 中保存用户的纬度和经度?
How would I go about saving a users latitude and longitude in my postgres table for a user using the html5 map?
所以我们要做的是,一旦用户点击 he/she 希望保存他们的位置,我们就会将其保存到我们的经度和纬度列中。感谢您的帮助和寻找我们!
https://github.com/rolaandoes/nexu/tree/dev
我们的 html 文件,其中的按钮用于保存我们的用户位置(纬度、经度)
<h1>Users#edit for <%= @user.id %></h1>
<p>Find me in app/views/users/edit.html.erb</p>
<p><button onclick="geoFindMe()" data-id="<%= @user.id %>"id="finder- btn">Geo-Coordnate My Position</button>Send Coordinates to DB</p>
<div id="out"></div>
<h2>Coordinates to DataBase!</br>lat, lon</h2>
<a href="#" onclick="this.style.backgroundColor='#990000'">Paint it red</a>
这是我们的js文件
$(function(){
$('#finder-btn').on('click', function (){
var currentUserId = $(this).attr('data-id')
$.ajax({
url: '/users/' + currentUserId,
data: { latitude: LatLng[0], longitude: LatLng[1] },
type: 'get',
success: function(data) {
console.log("Patch Succesful!")
},
error: function(err) {
console.log("Error Thrown")
}
});
});
});
//update location for current_user
LatLng = [];
console.log(LatLng);
var latitude = LatLng[0]
var longitude = LatLng[1]
function geoFindMe() {
var output = document.getElementById("out");
if (!navigator.geolocation){
output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
return;
}
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
var img = new Image();
img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";
// console.log(longitude);
// console.log(latitude);
//PLUCK into Location TABLE
// latitude = lat_Jon;
// longitude = lon_Jon;
LatLng.push(latitude);
LatLng.push(longitude);
output.appendChild(img);
};
function error() {
output.innerHTML = "Unable to retrieve your location";
};
output.innerHTML = "<p>Locating…</p>";
navigator.geolocation.getCurrentPosition(success, error);
}
我们的 users_controller 我们需要更新 table 上的经纬度位置
def update
# @user = User.find(session[:user_id])
user_id = current_user.id
@user = User.find(user_id)
@user.update_attributes(user_params)
puts @user.latitude
end
这里发生的一些事情可能会给您带来麻烦。
您正在执行 GET
而不是 POST
或 PUT
来尝试更新用户的纬度和经度。
尝试将您的 AJAX 调用从 type: 'get'
更改为 type: 'post'
这将有助于 Rails 确定将您的请求路由到哪个控制器操作(您想要的是 users_controller#update
在您的 users_controller
中,使用 params[:id]
查找用户,代码如下所示:
user = User.find(params[:user_id])
和你找到用户的方法一样,你需要从params中解析出纬度和经度。您的 user_params
方法正在假设您实际上并未执行的参数。它的东西你有看起来像的数据:
{ 'user': { 'longitude': '29.388', 'latitude': '187.39848' } }
但是你的参数看起来像:
{ 'longitude': '29.388', 'latitude': '182.3888' }
所以你需要做和上面一样的事情,使用params[:longitude]
和params[:latitude]
我会这样写:
class UsersController < ApplicationController
def update
user = User.find(params[:id])
user.update!(latitude: params[:latitude], longitude: params[:longitude])
end
end
所以我们要做的是,一旦用户点击 he/she 希望保存他们的位置,我们就会将其保存到我们的经度和纬度列中。感谢您的帮助和寻找我们!
https://github.com/rolaandoes/nexu/tree/dev
我们的 html 文件,其中的按钮用于保存我们的用户位置(纬度、经度)
<h1>Users#edit for <%= @user.id %></h1>
<p>Find me in app/views/users/edit.html.erb</p>
<p><button onclick="geoFindMe()" data-id="<%= @user.id %>"id="finder- btn">Geo-Coordnate My Position</button>Send Coordinates to DB</p>
<div id="out"></div>
<h2>Coordinates to DataBase!</br>lat, lon</h2>
<a href="#" onclick="this.style.backgroundColor='#990000'">Paint it red</a>
这是我们的js文件
$(function(){
$('#finder-btn').on('click', function (){
var currentUserId = $(this).attr('data-id')
$.ajax({
url: '/users/' + currentUserId,
data: { latitude: LatLng[0], longitude: LatLng[1] },
type: 'get',
success: function(data) {
console.log("Patch Succesful!")
},
error: function(err) {
console.log("Error Thrown")
}
});
});
});
//update location for current_user
LatLng = [];
console.log(LatLng);
var latitude = LatLng[0]
var longitude = LatLng[1]
function geoFindMe() {
var output = document.getElementById("out");
if (!navigator.geolocation){
output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
return;
}
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
var img = new Image();
img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";
// console.log(longitude);
// console.log(latitude);
//PLUCK into Location TABLE
// latitude = lat_Jon;
// longitude = lon_Jon;
LatLng.push(latitude);
LatLng.push(longitude);
output.appendChild(img);
};
function error() {
output.innerHTML = "Unable to retrieve your location";
};
output.innerHTML = "<p>Locating…</p>";
navigator.geolocation.getCurrentPosition(success, error);
}
我们的 users_controller 我们需要更新 table 上的经纬度位置
def update
# @user = User.find(session[:user_id])
user_id = current_user.id
@user = User.find(user_id)
@user.update_attributes(user_params)
puts @user.latitude
end
这里发生的一些事情可能会给您带来麻烦。
您正在执行
GET
而不是POST
或PUT
来尝试更新用户的纬度和经度。尝试将您的 AJAX 调用从
type: 'get'
更改为type: 'post'
这将有助于 Rails 确定将您的请求路由到哪个控制器操作(您想要的是users_controller#update
在您的
users_controller
中,使用params[:id]
查找用户,代码如下所示:user = User.find(params[:user_id])
和你找到用户的方法一样,你需要从params中解析出纬度和经度。您的
user_params
方法正在假设您实际上并未执行的参数。它的东西你有看起来像的数据:{ 'user': { 'longitude': '29.388', 'latitude': '187.39848' } }
但是你的参数看起来像:
{ 'longitude': '29.388', 'latitude': '182.3888' }
所以你需要做和上面一样的事情,使用
params[:longitude]
和params[:latitude]
我会这样写:
class UsersController < ApplicationController def update user = User.find(params[:id]) user.update!(latitude: params[:latitude], longitude: params[:longitude]) end end