I'm not sure why you are using loops here, but obviously I'm seeing just a
small part of your application.  And as for using JS to reload the page or
redirect (window.location), I would not recommend it unless there is not
another alternative.  Here is the big takeaway (imho)...  AJAX requests
will not cause a page reload or redirect etc..., so if you want that to
happen, consider a "normal" HTTP request.

Another important concept is the the $(function(){ /*code in here/* });
 means that the JS will only execute after the page loads.  This way the
HTML that the JS often needs to interact with is loaded before the JS tries
to execute.

As you pointed out, my previous example loaded the position coords into the
form every time the page was loaded.  I do not think that is an expensive
operation, but if you do not want that to happen, consider another option.
 You can have some JS code execute only when the form is submitted (when
the user clicks the "CHECK IN" button.  Here is a code example:

<script type="text/javascript">
// This code will execute after the HTML has finished rendering (page load)
$(function(){
  $('#check-in-form').submit(function(){
    if(navigator.geolocation){
      navigator.getCurrentPosition(function(position){
        $('#lng').val(position.coords.longitude);
        $('#lat').val(position.coords.latitude);
      });
    }
  });
});
// When the form is submitted, load the values from the
navigator.getCurrentPosition() function into our hidden form fields
</script>

<form action='/users/set_geolocation' id="check-in-form">
  <input type="hidden" name="latitude" id="lat" />
  <input type="hidden" name="longitue" id="lng" />
  <input type="submit" value="--> Check-In!" />
</form>


On Sat, Mar 1, 2014 at 1:08 PM, Ephraim Feig <[email protected]> wrote:

> No happy solution yet, but this is what I got so far.
>
> I have only spent a little time on the submit_tag suggestion (which I like
> a lot) but have not yet gotten it. That's just my ignorance of the
> intricacies of JQuery (oh but for more experience), but I am quite sure I
> will get it to pass the parameters. What concerns me with this method is
> that (I think) because I have to load the parameters before the submit call
> in order to have them ready for the form, I would have to execute
> navigator.geolocation ahead of time, and since I don't know when somebody
> will want to checkin, I would have to continually call it. I can avoid it
> by creating a splash page that gets called first only when one wants to
> checkin, and then do the submit in the splash page (not pretty). Even here,
> will the javascript not load the splash page?
>
> As for the link_to method, when I add  window.location.reload(true),
> strange things happen, depending on where in the script I add it. If I add
> it inside the most inner loop, the server signs me off (protect from
> forgery works). When I put it in the outer loop, the thing not just
> reloads, but it keeps reloading and reloading. I find it strange, because I
> have it as click(function). I think that I can work around the looping, but
> I don't like that at all. I feel to exposed to possible bad things
> happening (like an infinite loop in production).
>
> So, I am still working on this.
>
> Ben, thanks for the input.
>
>
> On Friday, February 28, 2014 11:47:44 AM UTC-8, Ephraim Feig 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.

Reply via email to