我一直在使用geocoder gem,但是因为ip_address中的geolocator是我试图通过使用和AJAX请求来解决这个问题的方法。试过POST方法,但是我收到路由错误所以尝试了GET而我的日志控制器窗口中的SQL似乎工作但我看不到页面本身出现的任何内容并且无法访问params [哈希。有人看到我做错了吗?
Company_addresses_controller.rb
def index_close_ones
#@company_addresses = CompanyAddress.near([50.50, 30.20], 10, units: :km) does work
@company_addresses = CompanyAddress.near([params[:lat], params[:lon]], 10, units: :km) #doesn't work
end
的routes.rb
get 'closetome' => 'company_addresses#index_close_ones'
welcome index.html.erb
<div class="jumbotron">
<h1>Welcome!</h1>
<p><a onclick="getLocation()" href="/closetome" class="btn btn-primary btn-lg" role="button">Close to you</a></p>
<!--For testing purposes-->
<p id="demo"></p>
<script type="text/javascript">
//for testing
var x = document.getElementById("demo");
function getLocation(){
if(navigator.geolocation){navigator.geolocation.getCurrentPosition(locate);}
else{alert("Not supported in this browser");}
}
function locate(pos) {
var crd = pos.coords;
var latitude = crd.latitude;
var longitude = crd.longitude;
$.ajax({
type: "GET",
url: "/closetome",
data: { lat: latitude, lon: longitude },
success: function(data){
return false},
error: function(data){
return false}
});
//for testing
x.innerHTML = "Latitude:" + latitude + "<br>Longitude:" +longitude;
}
</script>
</div>