开发者

Script block in content_for :in_script doesn't work

The script block begins with $('#map#{shop.id}')..... in content_for :in_script doesn't work.

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

I have this helper called in my view edit.html.erb with <%= edit_country_fields_template %>. But I have problem having the script code below added inline in the script of the view file:

$('#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 });

The end result is if shop.geocoded is true, the script code above will be shown in my view as inline script:

<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 });
    });

Thanks.


content_for does not output anything, but stores the HTML in a buffer for use in other places (like the header or footer of your page).

In your view or partial:

content_for :my_script do
  "abc"
end

Then in your layout:

yield :my_script

Without the yield, your content_for will not be displayed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜