简体   繁体   中英

How can I make an array of a block in rails?

I want to use to_sentence method on the block of code to return the names with , separated :

<% @products_in_category.each do |pic| %>
  <%= pic.name %>
<% end %>

How can I do this ?

<%= (@products_in_category.map {|x| x.name}).to_sentence %>

Replace each with map and it will return an array with the results of invoking the block on each element. So you can do

@products_in_category.map(&:name).to_sentence

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