用Ruby on Rails建立传单

时间:2023-01-29 23:30:59

I am attempting to get leaflet setup in my rails application properly, and I am having some trouble. I am following the steps outlined here Github Leaflet Repo. I have done the trivial stuff at the top and am now under the Headers heading.

我正在尝试在我的rails应用程序中获得传单安装,我遇到了一些麻烦。我正在遵循Github上的单张Repo所列出的步骤。我在顶部做了一些琐碎的事情,现在在标题下面。

I am using OpenStreetMaps, so my leaflet.rb file looks like this. (I had to create this file myself as it did not already exist)

我正在使用OpenStreetMaps,所以我的传单。rb文件是这样的。(我必须自己创建这个文件,因为它还不存在)

Leaflet.tile_layer = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
Leaflet.attribution = '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
Leaflet.max_zoom = 18

Now the view I am trying to insert the leaflet map in currently looks like this.

现在我正在尝试插入传单地图的视图是这样的。

<% provide(:title, 'Map') %>
<h1>Map</h1>
<div id="map"><%

    map(:center => {
    :latlng => [51.52238797921441, -0.08366235665359283],
    :zoom => 18
    })

%>
</div>
<p>Find me in app/views/dynamic_pages/map.html.erb</p>

When I fire up my rails server, there is just the empty space in the div i.e. no map. The div is setup with a height of 500px in my css file if that makes any difference, which I don't think it does.

当我启动rails服务器时,div中只有空空间,即没有映射。div在我的css文件中设置了500px的高度,如果这有什么区别的话,我不认为它有什么区别。

What am I doing wrong here?

我在这里做错了什么?

1 个解决方案

#1


3  

I'm sure you figured this out by now... But in case anyone else is stuck. Leaflet-rails documentation leaves out the details on how this happens, but the map helper is available in the views. It must be in <%= %> tags to display; the problem above is that the erb tag is missing the = it should look like this,

我相信你现在已经明白了……但万一有人被困住了。Leaflet-rails文档没有详细说明这是如何发生的,但是可以在视图中找到map helper。必须在<%= %>标记中显示;上面的问题是erb标签丢失了=应该是这样的,

<div id="map">
  <%=
    map(:center => {
      :latlng => [51.52238797921441, -0.08366235665359283],
      :zoom => 18
    })
  %>
</div>

As a side note, = in the erb tag tells the interpreter that the following code is an expression meant to be returned as a string to the document. An erb tage without the = like this <% %> will evaluate the code, but will not try to return it. For more see this blog post for more.

作为补充说明,erb标记中的=告诉解释器,以下代码是一个表达式,用于作为字符串返回到文档。没有这样的= <% >的erb参数将计算代码,但不会尝试返回它。更多详情请见此博客。

#1


3  

I'm sure you figured this out by now... But in case anyone else is stuck. Leaflet-rails documentation leaves out the details on how this happens, but the map helper is available in the views. It must be in <%= %> tags to display; the problem above is that the erb tag is missing the = it should look like this,

我相信你现在已经明白了……但万一有人被困住了。Leaflet-rails文档没有详细说明这是如何发生的,但是可以在视图中找到map helper。必须在<%= %>标记中显示;上面的问题是erb标签丢失了=应该是这样的,

<div id="map">
  <%=
    map(:center => {
      :latlng => [51.52238797921441, -0.08366235665359283],
      :zoom => 18
    })
  %>
</div>

As a side note, = in the erb tag tells the interpreter that the following code is an expression meant to be returned as a string to the document. An erb tage without the = like this <% %> will evaluate the code, but will not try to return it. For more see this blog post for more.

作为补充说明,erb标记中的=告诉解释器,以下代码是一个表达式,用于作为字符串返回到文档。没有这样的= <% >的erb参数将计算代码,但不会尝试返回它。更多详情请见此博客。