开发者

Inserting javascript values into erb code

开发者 https://www.devze.com 2023-02-12 08:31 出处:网络
I have a js.erb template with the following code: var latlng = new google.maps.LatLng(<%= session[:lat] %>, <%= session[:lng] %>);

I have a js.erb template with the following code:

var latlng = new google.maps.LatLng(<%= session[:lat] %>, <%= session[:lng] %>);

I'm now trying to do th开发者_运维技巧e opposite (insert javascript into ruby):

<% session[:lat] = javascript_tag("document.write(location.lat());") %>


I think you're missing the point of Javascript.

Ruby code (<% .. %> parts in your examples) is run on the server.

Javascript code (var latlng = new google.maps.LatLng(10, 20); and document.write(location.lat());) is executed in the browser.
It's also executed after ruby code, when server has processed request and generated result page already.

If you want to know value of location.lat() on the server, you'll have to send it from the browser to the server in AJAX request. Any popular Javascript library will help with that.


Maybe you could try <%= raw [ruby/rails code] %>
This link may help: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/a160d9c2e55cfe36

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号