简体   繁体   English

如何将ruby(rails)变量传递给Javascript?

[英]How can I pass a ruby(rails) variable into Javascript?

This is an app in rails. 这是rails中的应用程序。

Here's what I have in 'routes': 这是我在“路线”中的内容:

 resources :destinations

Here is what I have in 'destinations_controller': 这是我在'destinations_controller'中的内容:

def show
     @destination = Destination.find(params[:id])
end

Here's what I have in 'views/destinations/show.html.erb': 这是我在'views / destinations / show.html.erb'中的内容:

<h2><%= @destination.latitude %></h2>

<script>

    var dest_lat = <%= @destination.latitude %>

</script>

Here's what I have in 'application.js': 这是我在'application.js'中的内容:

var dest_location = {dest_lat};

This is the third time I've built this app. 这是我第三次构建这个应用程序。 Initially, I get no errors and all the coding works fine... for a few hours. 最初,我没有错误,所有编码工作正常...几个小时。 Then, without me changing anything, the value in @destination.latitude still appears in the h2 tag, but I start to receive the error: 然后,在没有我改变任何东西的情况下,@ destination.latitude中的值仍然出现在h2标签中,但我开始收到错误:

Uncaught ReferenceError: dest_lat is not defined

with a link that shows its use in 'application.js', and the app ceases to work. 使用链接显示其在'application.js'中的使用,该应用程序停止工作。

Why? 为什么?

Any help is appreciated. 任何帮助表示赞赏。

This isn't printing something useful for Javascript: 这不是打印对Javascript有用的东西:

<script>
    var dest_lat = <%= @destination.latitude %>
</script>

Try instead to change it to: 请尝试将其更改为:

<script>
    var dest_lat = '<%= @destination.latitude %>';
</script>

Here you can see that the quotes make the difference. 在这里你可以看到引号有所不同。

 var dest_lat = some_value; 

 var dest_lat = 'some_value'; console.log(dest_lat); 

Update: To make your data available in your application.js you can store them in a non-visible div, with content_tag, which will content your @destination.latitude and anything else you need to work 更新:要在您的application.js中提供数据,您可以使用content_tag将它们存储在不可见的div中,该content_tag将满足您的@destination.latitude以及您需要工作的任何其他内容

# your_view.html.erb
<%= content_tag :div, class: 'destination_latitude', data: {destination_latitude: @destination.latitude} do %>
<% end %>

Then you can access to them as a Javascript object: 然后您可以将它们作为Javascript对象访问:

document.addEventListener('DOMContentLoaded', function(e) {
  console.log($('.destination_latitude').data('destination-latitude'));
});

Check your rendered HTML output. 检查渲染的HTML输出。 As I said in my comment, I'm guessing that the script tag with the src pointing at your javascript file(s) is being called before the script tag with the embedded javascript. 正如我在评论中所说,我猜测在带有嵌入式javascript的脚本标记之前,正在调用src指向您的javascript文件的脚本标记。 The fix is to just change the order that they're declared on the page, but the specifics will depend on how your layouts, pages, and partials are setup (which I can't tell from your posted question). 修复只是更改它们在页面上声明的顺序,但具体取决于您的布局,页面和部分的设置方式(我从您发布的问题中无法分辨)。

When all is said and done, you'll have something like: 完成所有工作后,您将拥有以下内容:

<script type='text/javascript'>
  // Your embedded script
</script>
<script src='/path/to/application.js' type='text/javascript'></script>

in your rendered output. 在您的渲染输出中。

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

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