简体   繁体   English

Rails在单行块内部渲染部分

[英]Rails Render Partial Inside a One Line Block

I've got a feed of information that I want to simplify by using some partials. 我有一些信息,我希望通过使用一些部分来简化。 Basically, I want to do something along the lines of 基本上,我想做一些事情

<%= 2.times { |j| render 'item_expanded', item: @social_json_feed[i + j], index: (i + j) } %>

However, the above code does not render the partial. 但是,上面的代码不会呈现部分代码。 Instead, it returns the value "2" two times. 相反,它返回值“2”两次。 If I write the block as a do-end block it works as one would expect. 如果我将块写为do-end块,它就像人们期望的那样工作。

<% 2.times do |j| %>
  <%= render 'item_expanded', item: @social_json_feed[i + j], index: (i + j) %>
<% end %>

Produces the partial in the way I want. 以我想要的方式产生局部。 But if the do-end block works correctly a one-line block should be able to work correctly as well, right? 但是如果do-end块正常工作,那么单行块也应该能够正常工作,对吧? Is it simply something about how the one-line block is formatted or is it something deeper into Rails magic? 它只是关于单行块如何格式化或者是否更深入Rails魔术? Also, why does the first example of code return "2" twice?? 另外,为什么第一个代码示例会两次返回“2”?

It's because in first case it renders only return value of 2.times method call, which is 2 . 这是因为在第一种情况下它只渲染2 2.times方法调用的返回值,即2 You should simply use the second notation. 您应该只使用第二种表示法。

如果您向map添加调用,则应该能够将其全部保存在一行中。

<%= 2.times.map { |j| render 'item_expanded', item: @social_json_feed[i + j], index: (i + j) } %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM