简体   繁体   中英

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. If I write the block as a do-end block it works as one would expect.

<% 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? Is it simply something about how the one-line block is formatted or is it something deeper into Rails magic? Also, why does the first example of code return "2" twice??

It's because in first case it renders only return value of 2.times method call, which is 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) } %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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