代码之家  ›  专栏  ›  技术社区  ›  Victor

内容中的脚本块:脚本不起作用

  •  0
  • Victor  · 技术社区  · 15 年前

    脚本块以 $('#map#{shop.id}')..... content_for :in_script 不起作用。

    def edit_country_fields_template(city, shop)
      content_tag(:div, :class => "item") do
        "<div id='map#{shop.id}' class='map'></div>".html_safe +
       if shop.geocoded?
             content_for :in_script do
               $('#map#{shop.id}').gMap({ markers: [{ latitude: #{spot.lat},
         longitude: #{shop.lng},
         html: '_latlng' },
         { address: '#{shop.address_geo}',
           html: '#{shop.name}<br/><a href='http://maps.google.com/maps?q=#{shop.address_geo}' target='_blank'>See Full Map</a>' },
          ],
          zoom: 16 });
              end
           end
      end
    end
    

    我有个助手在我看来 edit.html.erb 具有 <%= edit_country_fields_template %>

    $('#map#{shop.id}').gMap({ markers: [{ latitude: #{spot.lat},
         longitude: #{shop.lng},
         html: '_latlng' },
         { address: '#{shop.address_geo}',
           html: '#{shop.name}<br/><a href='http://maps.google.com/maps?q=#{shop.address_geo}' target='_blank'>See Full Map</a>' },
          ],
          zoom: 16 });
    

    最终结果是 if shop.geocoded true ,上面的脚本代码将在我的视图中显示为内联脚本:

    <script type="text/javascript">
    $(document).ready(function(){
        $('#map1').gMap({ markers: [{ latitude: -1.030503,
         longitude: 1.340594,
         html: '_latlng' },
         { address: '200 Good Street, California, United States of America',
           html: 'California Restaurant<br/><a href='http://maps.google.com/maps?q=200 Good Street, California, United States of America' target='_blank'>See Full Map</a>' },
          ],
          zoom: 16 });
        });
    

    谢谢。

    1 回复  |  直到 15 年前
        1
  •  0
  •   Ariejan    15 年前

    content_for 不输出任何内容,但将HTML存储在缓冲区中以供在其他地方使用(如页面的页眉或页脚)。

    在您看来或部分:

    content_for :my_script do
      "abc"
    end
    

    然后在布局中:

    yield :my_script
    

    没有 yield ,您的 内容 将不显示。