1

How do you loop through a ruby variable in a Haml javascript tag? I have a collection of @reviews that I need to loop through. How do you loop through this collection within a javascript tag?

  - content_for :javascript do

      = javascript_include_tag "jquery.expander"
        :javascript
        - @reviews.each do |review|
         $("#share#{review.id}").live('click', function() {
            FB.ui(
              {
                method: 'feed',
                name: '#{@product.name}',
                link: '#{@product.page_url(@locale)}',
                picture: '#{@product.image_url(@locale)}',
                caption: 'Dell Social Shop',
                description: '#{@product.description}'
              },
              function(response) {
                if (response && response.post_id) {
                  alert('Post was published.');
                } else {
                  alert('Post was not published.');
                }
              }
            );
          });}
1

2 Answers 2

1

Try putting the - @reviews.each do |review| portion outside the = javascript_include_tag "jquey.expander" block, instead of inside it. Where it's sitting right now, it's probably being interpreted as javascript instead of Ruby, since it's indented under the :javascript line.

Sign up to request clarification or add additional context in comments.

Comments

1

I found out that for Javascript code Erb templating works better than Haml templating. So i'd extract the loop body into an .erb partial, and do something like

= javascript_include_tag "jquery.expander"
= javascript_tag render(:partial => 'review.js.erb', :collection => @reviews)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.