5

I am trying to grab a users location via HTML5 and pass it to my rails controller.

I have the JavaScript below which calls .value on the element and sets it equal to the respective position. I would then like to submit these values through a hidden form field and pass it to my location controller so I can populate locations based on the users position. I know this has been done before and I have seen a few posts on it, but I have not had success.

navigator.geolocation.getCurrentPosition(GeoL);
function GeoL(position) {
  document.getElementById('lat').value = position.coords.latitude;
  document.getElementById('lon').value = position.coords.longitude;
}

<%= form_tag locations_path, :method=>'post' do %>
  <%= hidden_field_tag 'lat', value = ''%>
  <%= hidden_field_tag 'lon', value = ''%>
  <%= image_submit_tag 'loc.png'  %>
<% end %>

def create
 "What goes here? Grab params? Is this the right action to send it to?"
end

I am currently getting the error you see in this screen shot below. I think my form may need some work, plus I need to add code in my controller to grab the values. As you can see a little lost, any advice would be great.

Error Message

Danny Sullivan
  • 277
  • 2
  • 12
  • well it seems your params (lat and lon) are being sent correctly, what do you want to do next, you didn't exactly post the error message. – Mohammad AbuShady Jan 28 '15 at 21:16
  • Right, sorry about that. "param is missing or the value is empty: location" is the error message. Ideally I would like to pass them to my controller so I can render them in my view, have the controller filter the results based on their location. I have it working right now with the geocoder gem, but it isn't accurate using the ip address. – Danny Sullivan Jan 29 '15 at 01:53
  • so location is a model in your system? and what gem are you using ? – Mohammad AbuShady Jan 29 '15 at 01:56
  • Yes I only have one model currently Location. I first used the ruby geocoder gem, which I am using to geolocate the locations, but for getting the user proximity it hasn't been very accurate. So I am trying to use HTML5 and pass it to a controller. I would like to be able to display, "You are 2.2miles from this location" or something like that. But right now with geocoder it is off quite a bit. – Danny Sullivan Jan 29 '15 at 02:11
  • i recommend `geokit-rails`, its a lot easier, i could provide an example if u want, anyways, accuracy of the location depends on the device, whether it has access to cellular networks/ wifi or gps – Mohammad AbuShady Jan 29 '15 at 02:35

1 Answers1

0

As i understand you, you would like to show locations based on the users position. That means you locate the user like you already did, write lat and lon into the hidden_form_fields and send this form via ajax to the index (!) action of your locations_controller (not the create action).

With geocoder the controller looks something like that:

@locations = Location.near([params[:lat], params[:lon]])
.
.
.
respond_to do |format|
 format.html {}
 format.js {}
end

Then you only have to insert your new @locations into your index view

Tom
  • 520
  • 2
  • 17
  • Thanks @Tom. I didn't need to use ajax, just had to set up a route so the form was submitting a post request to the index action, instead of the create action. From there I used params as you suggested in my index action and it's working perfectly. `match "locations" => "locations#index", :via => [:post]` is what was needed. – Danny Sullivan Feb 01 '15 at 18:04