简体   繁体   English

将Javascript变量保存到Ruby中

[英]Saving Javascript Variable Into Ruby

I am using the HTML 5 Geolocation code ( http://www.w3schools.com/html/html5_geolocation.asp ) to get the users location. 我正在使用HTML 5地理位置代码( http://www.w3schools.com/html/html5_geolocation.asp )获取用户位置。

...
function showPosition(position)
  {
    x.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
  }
</script>

I want to create a variable in ruby that holds the value of position.coords.latitude and position.coords.longitude, does anyone know the most effective way to capture this information and save it as a ruby variable? 我想在ruby中创建一个包含position.coords.latitude和position.coords.longitude的值的变量,有人知道捕获此信息并将其另存为ruby变量的最有效方法吗?

Well there are 2 ways you can do this - 嗯,有两种方法可以做到这一点-

  1. Make ajax call 拨打ajax电话

     var latitude = position.coords.latitude; var longitude = position.coords.latitude; $.ajax({ url: "/<some_route>?latitude="+latitude+"&longitude="+longitude, success: function(data) { // do something here like for example replace some arbitrary container's html with the stuff returned by server. $("#container").html(data); } }); 

    Access it on the server side using params hash 使用params hash在服务器端访问它

     latitude = params[:latitude] longitude = params[:longitude] 
  2. Set a location cookie on the client side and access it on the server side. 在客户端设置位置cookie,并在服务器端访问它。

     var latitude = position.coords.latitude; var longitude = position.coords.longitude; document.cookie = "cl=" + latitude + "&" + longitude + ";"; 

    And in your controller you can access this - 在您的控制器中,您可以访问-

     current_location = cookies[:cl] unless current_location.nil? latitude = current_location.split('&')[0] longitude = current_location.split('&')[1] end 

Having said that you should prefer option 1 because cookies are sent to the server with each HTTP request and additionally you might need to inform users that tracking technologies are used. 话虽这么说,您应该选择选项1,因为cookie是随每个HTTP请求发送到服务器的,另外,您可能需要通知用户使用了跟踪技术。

A request have, on high, the following steps: 一个请求主要包括以下步骤:

  1. Browser requests a page to the server 浏览器向服务器请求页面
  2. The server processes the request and gives back the page, which might include Javascript code 服务器处理请求并返回页面,该页面可能包含Javascript代码
  3. The browser executes the javascript code and keep working on it 浏览器执行javascript代码并继续进行处理

The question of how to have a variable that is only present in 3 on step 2 , this is impossible. 如何拥有仅在步骤2 3存在的变量的问题,这是不可能的。

An alternative is simply add an ajax call (that is, repeat steps 1 to 3, but instead of "Browser" read "Javascript code") that sends this information to the server to save in the database or something, but you will still have to program your logic in javascript. 一种替代方法是简单地添加一个ajax调用(即,重复步骤1至3,而不是“ Browser”阅读“ Javascript代码”),该信息会将此信息发送到服务器以保存在数据库中或其他内容中,但是您仍然可以在javascript中编写逻辑程序。

does anyone know the most effective way to capture this information and save it as a ruby variable? 有谁知道捕获此信息并将其另存为红宝石变量的最有效方法?

What is your measurement of effective ? 您的有效衡量标准是什么? The data won't be missing a random digit when it gets to the server? 数据到达服务器时会不会丢失随机数字吗?

Making a jQuery ajax request is easiest : 发出jQuery ajax请求是最简单的

var lat = 10;
var lon = 20;

function onsuccess(data, textStatus, jqXHR) {  
    alert(textStatus); 
}

$.post(
    "http://localhost:3000/some/route",
    {'lat': lat, 'lon': lon},
    onsuccess   //optional
);

The latitude and longitude can be retrieved inside your action from the params hash: 纬度和经度可以从params哈希中检索到:

lat = params[:lat]
lon = params[:lon]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM