【问题标题】:rails 3: How can I concatenate a number to a symbol?rails 3:如何将数字连接到符号?
【发布时间】:2012-08-28 09:57:54
【问题描述】:

我希望通过在 1..5 循环中使用 i@comment1 更改为 @comment2。我有以下非常重复的代码。我希望把它干掉。

您好,我正在使用acts_as_commentable_with_threading。我基本上是在遍历所有 cmets 并检查该评论是否有孩子。如果是这样,打印出孩子,同时检查这些孩子是否有孩子。所以我计划深入几个层次,因此@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 %>

【问题讨论】:

  • 您能否详细说明“@comment1 使用i 更改为@comment2”是什么意思?
  • 我的视图有很多重复的代码,我正试图把它们干掉。我正在打印出带螺纹的 cmets。在我看来,这一直在重复......
  • 这是一种代码异味,如果您有一系列用于相同目的的 ivars,则应考虑使用一个哈希或数组。
  • 这段代码不是打印了8次吗?
  • 可能不是实际代码。

标签: ruby-on-rails ruby string recursion


【解决方案1】:

您可能正在寻找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

【讨论】:

  • 产量无法正常工作。实际上该块需要通过。我很快就会用正确的代码回来。
  • 已更新...希望现在它能按预期工作。让我们知道状态:)
  • 嗨,代码有效,非常感谢!我有一个小问题,我似乎无法弄清楚。我在哪里可以插入这个 div“
    ”。这个 div 将封装 children/s,然后在没有更多孩子时结束 div。我认为它应该属于助手。
  • 如果它总是&lt;div class="span7 offset1-1 pcomment"&gt; 那么你的代码应该可以工作。似乎已经包含了您的孩子的评论。但是,concat '&lt;div class="span7 offset1-1 pcomment"&gt;'.html_safe 可能会更干净一些。但如果不是,请添加您想要的确切 html 作为输出。
【解决方案2】:

您在其中定义此代码的庄园相当糟糕,您应该考虑将@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 %>

这绝对是值得了解的事情,但在这种情况下,我强烈建议您将注释实例变量转换为数组!

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签