简体   繁体   English

Ruby On Rails:如何在每次刷新页面时自动提交隐藏表单(无按钮)

[英]Ruby On Rails: how to submit a hidden form automatically every time I refresh a page (no buttons)

thanks to whoever will help me! 多亏谁能帮助我!

I'm currently getting the coordinates of my user every time he enters in a specific page , and I'm trying to store these coordinates updating the user model, that has the fields "latitude' and 'longitude' in it. Currently it works if the user press the button "I'm here", I'm trying to auto-submit the form and hide the button, but I'm not having success. To be clear, I'd like that every time someone enters this specific page, his user model will be updated with the new "latitude" and "longitude" without him doing anything or seeing anything. 目前,我每次用户进入特定页面时都会得到我的用户的坐标,并且我正在尝试存储这些坐标以更新用户模型,该模型中包含“纬度”和“经度”字段。如果用户按下“我在这里”按钮,则我试图自动提交表单并隐藏该按钮,但是我没有成功,请注意,我希望每次有人输入此信息时特定页面上,他的用户模型将被更新为新的“纬度”和“经度”,而无需他做任何事情或看到任何事情。

*the $("#new_coordinates").submit() where "new coordinates" is the form id is currently not working, that's why the f.submit with the button is still there. * $(“#new_coordinates”)。submit()(其中“新坐标”是表单ID)当前不起作用,这就是带有按钮的f.submit仍然存在的原因。

Here's my code 这是我的代码

<p id="geoloc"></p>
<script>
    var x = document.getElementById("geoloc");

    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(getCoordinates);
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }

    function getCoordinates(position) {
        var new_latitude = position.coords.latitude;
        var new_longitude = position.coords.longitude;

        $(document).ready(function () {
                    $("#latitude_field").val(new_latitude);
                    $("#longitude_field").val(new_longitude);
                    $("#new_coordinates").submit();


                }
        )
    }

    getLocation()


</script>

<%= form_for(current_user, :id=> "new_coordinates") do |f|  %>
    <%=f.hidden_field :latitude ,:id=> "latitude_field" %>
    <%=f.hidden_field :longitude ,:id=> "longitude_field" %>
    <%=f.submit "I'm here", class: "btn btn-small btn-primary jsbuttons"%>

<%end%>

You can make ajax calls to controller to update the user data. 您可以对控制器进行ajax调用以更新用户数据。 Just consider i want to update the user data when the page loads, you can use jquery method .ready 只考虑我想在页面加载时更新用户数据, you can use jquery method .ready

$(document).ready(function(){
   $.ajax({url: "/update_user", type: "POST", data: {name: "xxx", id: 5});

});

In case you want to go with javascript, use 如果您想使用javascript,请使用

        <body>
        HTML Content
        <script>
            (function() {
           $.ajax({url: "/update_user", type: "POST", data: {name: "xxx", id: 5});             
        })();
        </script>
        </body>

Also, you have to add the '/update_user' in routes.rb to handle the requests to reach the respective action in controller. 同样,您必须在routes.rb中添加“ / update_user”以处理请求以达到控制器中的相应操作。

Update: 更新:

Once you receive the values in controller 一旦收到控制器中的值

users_controller.rb users_controller.rb

def update_user
  #you receive all the data in params
end

use the params to retrieve the data and use accordingly. 使用参数来检索数据并相应地使用。 params is a hash and you can access the data from keys. params是一个哈希,您可以从键访问数据。 Then you can pass those values directly to the method as you want. 然后,您可以根据需要将这些值直接传递给方法。 If you want it something like on the model just to access, you can go with attr_accessor. 如果您希望只访问模型上的内容,则可以使用attr_accessor。 Read about that though i don't feel you really need that. 请阅读有关内容,尽管我不觉得您确实需要它。

Just read the params in action and pass the values accordingly. 只需阅读实际的参数并相应地传递值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM