So sorry, everyone... hit submit by accident :(
continued....
<%= submit_tag %>
<% end %>
2. Create a jQuery script that loads the lat and lng values into the form
when the page loads. Something like:
jQuery(function(){
// JS code to get lat and lng from navigator object and set them into
hidden form field values
});
3. When the user submits the form, it will hit your controller with the
proper values and then redirect as you expect because it is a plain old
HTTP request, not an AJAX request.
Basically you are doing the same things as before, but with a regular HTTP
request
On Fri, Feb 28, 2014 at 3:55 PM, Ben Wanicur <[email protected]> wrote:
> Hi Ephraim
>
> My last answer was a bit vague. Here are more detailed steps to deal with
> your issue. This is going on the assumption that you are getting your
> Geolocation info through a javascript function.
>
>
> 1. Create form with only hidden fields for GeoLocation data, looks like
> only latitude and longitude in your case. So something like
>
> <%= form_tag '/users/set_geolocation/' (or use rails helper method) do %>
> <%= hidden_field_tag :latitude, :id => 'latitude-field' %>
> <%= hidden_field_tag :longitude, :id => 'longitude-field %>
>
>
>
> On Fri, Feb 28, 2014 at 3:48 PM, Ephraim Feig <[email protected]>wrote:
>
>> Ben,
>>
>> Thank you. It makes total sense. Unfortunately, I am obviously missing
>> the correct syntax for doing this. I've tried a lot of different ways, but
>> no luck so far. I'll keep trying.
>>
>> Ephraim
>>
>>
>> On Friday, February 28, 2014 12:38:28 PM UTC-8, Benjamin Wanicur wrote:
>>
>>> Hi Ephraim
>>>
>>> You are issuing an AJAX call to your UsersController. This cannot
>>> redirect the page directly. You could (I would not recommend this) have a
>>> success callback in your jQuery method that uses the window.location =
>>> "/some_url_here" and that would redirect your browser.
>>>
>>> Perhaps a better approach (if you want the page to redirect) is create a
>>> form with a few hidden form fields (for your geolocation data). The submit
>>> button on the form could trigger your JS function and then submit (not AJAX
>>> call) to your UsersController#set_geolocation method. This would be a
>>> plain-old HTTP request (POST request) and it will follow the Rails
>>> redirect_to method.
>>>
>>> Cheers
>>>
>>> Ben W
>>>
>>>
>>> On Fri, Feb 28, 2014 at 11:47 AM, Ephraim Feig <[email protected]>wrote:
>>>
>>>> 0 down vote
>>>> favorite<http://stackoverflow.com/questions/22102460/page-does-not-update-after-navigator-geolocation-call-in-rails-check-in-check#>
>>>>
>>>> I am using standard HTML5 location services. A user comes into a cafe,
>>>> opens the app, clicks on "Check-in!" and the app gets the location and uses
>>>> it until the user later clicks "Check-out!".
>>>>
>>>> In my viewer, I call on a script containing navigator.geolocation and
>>>> it does its job. I successfully pass the latitude and longitude parameters
>>>> to my controller and it updates everything. But when all is done and I
>>>> redirect_to root_url, the new page does not refresh (I know at some point I
>>>> should do it more elegantly and just update the link part, but for now, I
>>>> just want to get this going and understand what is going on). Here is my
>>>> code:
>>>>
>>>> In my viewer-
>>>>
>>>> <% if (@voterinfo.checkin == 0) %>
>>>> <li>
>>>> <h4> <%= link_to "-->Check-In!", "#", :id => 'findMe'
>>>> %> </h4>
>>>> <script>
>>>> $(function(){
>>>> $("a#findMe").click(function(){
>>>> if (navigator.geolocation) {
>>>>
>>>> navigator.geolocation.getCurrentPosition(function (position) {
>>>> $.post('/users/set_geolocation/',
>>>> {latitude: position.coords.latitude,
>>>> longitude:
>>>> position.coords.longitude,
>>>> dataType: 'float'});
>>>> }, function () {
>>>> alert('We couldn\'t find your position.');
>>>> });
>>>> } else {
>>>> alert('Your browser doesn\'t support
>>>> geolocation.');
>>>> }
>>>> });
>>>> })
>>>> </script>
>>>> </li>
>>>> <% else %>
>>>> <li> <h4><%= link_to "--->CHECK-OUT!", :controller => :users,
>>>> :method => :set_geolocation %></h4></li>
>>>> <% end %>
>>>> </ul>
>>>> <% end %>
>>>>
>>>> And here is the relevant part of my users_controller-
>>>>
>>>> def set_geolocation
>>>> @user = current_user
>>>> @voterinfo = Voterinfo.find_by_user_id(@user.id)
>>>> if (@voterinfo.checkin == 0)
>>>> @user.update_attributes(:longitude => params['longitude'], :latitude
>>>> => params[:latitude])
>>>> @user.save(validate: false)
>>>> sign_in(@user)
>>>> @voterinfo.checkin = 1
>>>> @voterinfo.save
>>>> redirect_to root_url
>>>> else
>>>> @voterinfo.checkin = 0
>>>> @voterinfo.save
>>>> redirect_to root_url
>>>> end
>>>>
>>>> end
>>>>
>>>> Thank you for any help here.
>>>>
>>>>
>>>> --
>>>> --
>>>> SD Ruby mailing list
>>>> [email protected]
>>>>
>>>> http://groups.google.com/group/sdruby
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "SD Ruby" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>>
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>
>>> --
>> --
>> SD Ruby mailing list
>> [email protected]
>> http://groups.google.com/group/sdruby
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "SD Ruby" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
--
--
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
---
You received this message because you are subscribed to the Google Groups "SD
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.