gpt4 book ai didi

javascript - 错误:How can I get the params from controller without reloading the browser?

转载 作者:行者123 更新时间:2023-12-03 06:55:48 25 4
gpt4 key购买 nike

我现在正在使用一些 gmaps API,我通过 gem“gon”传递 lat 和日志。由于我希望标记是最新的,因此我需要最新的纬度和日志。如何在不重新加载整个页面的情况下将参数从 Controller 传递到 view.html?

Controller .rb

@targetrecords.each do |targetrecords|

pnts[i][0]=targetrecords.latitude
pnts[i][1]=targetrecords.longitude
pnts[i][2]=targetrecords.address
pnts[i][3]=targetrecords.created_at

end

gon.pnts=pnts

index.html.erb

<script type="text/javascript">

var centlat = (gon.pnts[0][0]+gon.pnts[1][0]+gon.pnts[2]
[0]+gon.pnts[3][0])/4;
var centlng = (gon.pnts[0][1]+gon.pnts[1][1]+gon.pnts[2]
[1]+gon.pnts[3][1])/4

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 14,
center: new google.maps.LatLng(centlat,centlng),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

</script>

View 中还有更多内容,但只是类似的内容,我希望通过单击或其他操作来更新它。

最佳答案

使用 gem gon,如果您正确遵循了设置,则可以在 Controller 中执行此操作:

def your_action
gon.your_variable = your_ruby_variable
end

现在您可以在 JavaScript 中访问它。要尝试一下,您可以这样做:

console.log(gon.your_variable)

您应该能够在浏览器的控制台中看到它。

您还可以使用 ajax 请求,但既然您询问了 gon,这应该可以工作。

编辑:要在第一页加载后获取更新,您可以使用 gon watch https://github.com/gazay/gon/wiki/Usage-gon-watch

关于javascript - 错误:How can I get the params from controller without reloading the browser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37281141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com