2

I decided to update my templating technology to HAML. I'm new to it, and wondered if this is possible at all:

:javascript    
  <% @checkins.each do |checkin| %>
    var latLng = new google.maps.LatLng(<%= checkin.latitude %>, <%= checkin.longitude %>);

    var marker = new google.maps.Marker({
        position: latLng, 
        map: map,
        title: '<%= escape_javascript(checkin.title) %>'
    });

    google.maps.event.addListener(marker, 'click', function() {
      document.location = '<%= checkin_path(checkin) %>';
    });    
  <% end %>

It's easy to see that the parts of the code I'm having problems with are the ones related to Ruby code

<% @checkins.each do |checkin| %>

or

document.location = '<%=checkin_path(checkin) %>';

I tried to change the latter to:

- @checkins.each do |checkin|

but it's not working out so far. I already read many other questions here about the same topic, but I haven't really been able to find a 'straight-forward' answer.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
jlstr
  • 2,986
  • 6
  • 43
  • 60
  • Look at this http://stackoverflow.com/questions/5864500/how-to-run-ruby-in-haml-in-javascript-definition – Awea Feb 07 '12 at 18:52
  • possible duplicate of [Is there a way to use a Ruby loop inside of HAML's :javascript region?](http://stackoverflow.com/questions/2962119/is-there-a-way-to-use-a-ruby-loop-inside-of-hamls-javascript-region) – the Tin Man Feb 07 '12 at 19:19
  • Yes, those are similar question I did read before posting, but like I said did not address the issue directly IMHO. sorry, – jlstr Feb 07 '12 at 20:21

1 Answers1

1

Try replacing <% @checkins.each do |checkin| %> with

#{ @checkins.each.do |checkin| 

and replace your end statement with }

Also replace your <%= %> with just checkin.whateverproperty

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Webjedi
  • 4,677
  • 7
  • 42
  • 59
  • Hmmm, that doesn't seem to work. Am I missing something because with that change, I get new errors in the following JavaScript lines. ie. syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' var latLng = new google.maps.LatLng(checkin.latitude... ... and more. – jlstr Feb 07 '12 at 20:23
  • Try simplfying the loop and just do like var mytext= "Bob" if that works then it's the place where you are including the checkin.whateverproperty...I think the syntax for that is like #{checkin.whateverproprty} – Webjedi Feb 07 '12 at 22:09