Ctpelnar1988
Ctpelnar1988

Reputation: 1255

How to get rid of array after .map method

I am instantiating a variable like so:

Searches Controller:

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

Searches View

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

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

How can I get rid of the array at the end?

Upvotes: 0

Views: 76

Answers (3)

oreoluwa
oreoluwa

Reputation: 5623

You should change from .map to .each since you aren't trying to change the value of the array. You should be able to fix with this:

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

The <%= means you want to print the value

Upvotes: 1

Simone Carletti
Simone Carletti

Reputation: 176362

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

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

to

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

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

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 %>

Upvotes: 1

Kumar
Kumar

Reputation: 3126

Change <%= to <%

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

Upvotes: 1

Related Questions