0

The following form is intended to submit data using HTML5 geolocation data.

The javascript

function geoOk(position)
{
  default_latitude = position.coords.latitude;
  default_longitude = position.coords.longitude;
  $('#my_latitude').html(default_latitude);
  $('#my_longitude').html(default_longitude);

properly generates data, which can be displayed in the view:

<span id="my_latitude"></span> => <span id="my_longitude"></span>

Now the form should generate params based on these JS generated variables. Hidden values are not possible as ERBs are generated server-side before the jasvascript generates any data.

<%= form_tag(:find_by_my_locations, method: :post) do %>
  <%= check_box_tag :location_ok, params[:location_ok] %><%= t('my_location_confirm') %><br />
  <%= submit_tag %>
<% end %>

How should can the params be generated for the next controller action?

jQuery is enabled on this application.

Edit

document.querySelector('#form_latitude').value = position.coords.latitude;
document.querySelector('#form_longitude').value = position.coords.longitude;

and form

  <%= hidden_field_tag "form_longitude" %> 
  <%= hidden_field_tag "form_latitude" %> 

worked without requiring the document ready JS bloc

1 Answer 1

1

Can you create another function once the document has loaded which can retrieve the required values via jquery and add them to a hidden field.

  $(document).ready(function(){
    var longitude = $("#longitude").text();
    $("#hidden_field").val(longitude);
  });
Sign up to request clarification or add additional context in comments.

8 Comments

the value is definitely not part of the params. The submit_tag may need an onClick handler?
I'm not clear what's not working. As part of your rails view include a hidden field in the form which has an id e.g. <%= hidden_field_tag "form_longitude" %>. You can update the value of this field as part of the function geoOk. $('#form_longitude').val(default_longitude);
Maybe I was not clear in my explanations. the form is being generated server-side. The javascript is being executed client-side and the the form cannot have its value defined as it does not yet exist. so somehow the variables needs to be passed only when a button is clicked, as then the value 'exists.
How is the function geoOk triggered? If it happens after the document is loaded, as long as the value of the hidden field is updated after the geoOk function is called it should work. I did a test and the updated value gets passed in the params hash.
Premise: when it come sot javascript, I am dense... getting to update the hidden_field via JS is truly opaque to me. do you have a JS fiddle where this test is apparent so I can try and understand it? THanx
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.