如何在.map方法之后摆脱数组 [英] How to get rid of array after .map method

查看:40
本文介绍了如何在.map方法之后摆脱数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样实例化一个变量:

I am instantiating a variable like so:

搜索控制器:

@search_results = Business.near(params[:search_zip], params[:radius]).to_a

搜索视图

<%= @search_results.map do |sr| %>
  <%= sr.business_name %>
<% end %>

=> PetStore FoodStore BeautyStore ClothingStore ["\n", "\n", "\n", "\n"]`

最后怎么去掉数组?

推荐答案

打印数组是因为您使用的是 <%= 而不是 <%.改变

The array is printed because you are using <%= instead of <%. Change

<%= @search_results.map do |sr| %>
  <%= sr.business_name %>
<% end %>

<% @search_results.map do |sr| %>
  <%= sr.business_name %>
<% end %>

map 返回块的评估.由于 <%=.

map returns the evaluation of the block. The returned value is then printed because of <%=.

另请注意,您不需要 map每个 就足够了,在这种情况下会节省您的资源:

Also note you don't need a map, each is sufficient and will save you resources in this case:

<% @search_results.each do |sr| %>
  <%= sr.business_name %>
<% end %>

这篇关于如何在.map方法之后摆脱数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆