繁体   English   中英

ROR:如何在不重新加载浏览器的情况下从控制器获取参数?

[英]ROR:How can I get the params from controller without reloading the browser?

我现在正在使用某些gmaps API,并通过lat并通过gem“ gon”登录。 由于我希望标记是最新的,因此我需要最新的经纬度和对数。 如何在不重新加载整个页面的情况下将参数从控制器传递到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>

视图中还有更多内容,但是就像我想要通过单击或其他方式使它更新的内容一样。

使用宝石gon,如果您正确地遵循了设置,则可以在控制器中执行以下操作:

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

暂无
暂无

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

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