1

1..5ループでを使用して@comment1に変更したいと思います。私はかなり反復的な次のコードを持っています。私はそれを乾かしたいと思っています。@comment2i

こんにちは、acts_as_commentable_with_threadingを使用しています。私は基本的にすべてのコメントをループし、そのコメントに子があるかどうかを確認しています。もしそうなら、それらの子供たちが子供を持っているかどうかを確認しながら子供たちを印刷してください。だから私はいくつかのレベルを深くすることを計画しているので、@ comment1,2,3など...どうすればこれを乾かすことができますか?どうやって再帰?そうでない場合は、たとえば、数レベル深くして、コメントのインデントを@comment5で終了することができます。

編集!

サミロンありがとう!

これが更新されたヘルパー関数です...

    def show_comments_with_children(comments)
    
     comments.each do |comment|
         
         yield comment
         if comment.children.any?
           concat <<-EOF.html_safe
                <div class="span7 offset1-1 pcomment">
           EOF
            show_comments_with_children(comment.children) {                 |x| yield  x } #Dont worry, this will not run another query :)
               concat <<-EOF.html_safe
                    </div>
               EOF
         end   
     end
  end

<div class="span7 offset1-1 pcomment">
<% @comment1 = comment.children%>
<% for comment in @comment1 %>
 <%= render "comment_replies", :comment => comment %>
            
<div class="span7 offset1-1 pcomment">
<% @comment2 = comment.children%>
<% for comment in @comment2 %>
<%= render "comment_replies", :comment => comment %>

<div class="span7 offset1-1 pcomment">
<% @comment3 = comment.children%>
<% for comment in @comment3 %>
<%= render "comment_replies", :comment => comment %>

                <% end %>
            </div>

...。

<%(1..5).each do |i| %>
  <% @comment1 = comment.children%>
  <% for comment in @comment1 %>
    <%= render "comment_replies", :comment => comment %>
  <% end %>
<% end %>
4

2 に答える 2

4

おそらくあなたは探していinstance_variable_setます。

# Following snippet is not TESTED. It is here to just demonstrate "instance_variable_set"
<%(1..5).each do |i| %>
  <% instance_variable_set("@comment#{i}", comment.children) %>
  <% for comment in instance_variable_get("@comment#{i}") %>
    <%= render "comment_replies", :comment => comment %>
  <% end %>
<% end %>

しかし、間違いなくこれは推奨されるアプローチではありません。コントローラコードと、ビューで達成したいことを共有できます。それを適切に乾燥させるための何らかの方法が必要です。あなたの投稿では、あなたは常に得ていcomment.childrenます。本当か?


実際の解決策:

ビューコードは次のようになります

#0th level is the top level
<% show_comments_with_children(@comments, 0) do |comment, level|%>
   <!-- #Use level to differ the design for different depth-->
   <%= render "comment_replies", :comment => comment %>
<%end%>

このヘルパー関数をヘルパー関数に追加show_comments_with_childrenします。どっちが。

def show_comments_with_children(comments, level)
   comments.each do |comment|
       yield comment, level
       if comment.children.any?
           show_comments_with_children(comment.children, level+1) {|x, l| yield x, l} #Dont worry, this will not run another query :)
       end
   end
end
于 2012-09-03T17:24:41.583 に答える
0

このコードを定義しているマナーはかなり貧弱であり、@commentそれぞれの独立変数としてではなく、配列として定義することを検討する必要があります@comment1, @comment2, etc.

そうは言っても、あなたが探しているのはinstance_variable_get()方法です

<(1..5).each do |i| %>
  <% instance_variable_set("@comment#{i}", comment.children) %>
  <% for comment in instance_variable_get("@comment#{i}") %>
    <%= render "comment_replies", :comment => comment %>
  <% end %>
<% end %>

これは確かに知っておくとよいことですが、この場合、コメントインスタンス変数を配列に変換することを強くお勧めします。

于 2012-09-03T17:29:00.217 に答える