如何在Haml javascript标记中循环遍历ruby变量?

时间:2011-09-16 18:57:05

标签: javascript ruby loops haml

如何在Haml javascript标记中循环遍历ruby变量?我有一个@reviews的集合,我需要循环。如何在javascript标记中循环遍历此集合?

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

2 个答案:

答案 0 :(得分:1)

尝试将- @reviews.each do |review|部分放在= javascript_include_tag "jquey.expander"块之外,而不是放在其中。它现在坐在哪里,它可能被解释为javascript而不是Ruby,因为它在:javascript行下缩进。

答案 1 :(得分:1)

我发现对于Javascript代码,Erb模板比Haml模板效果更好。所以我将循环体提取为.erb部分,并执行类似

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