0

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?

Ctpelnar1988
  • 1,235
  • 3
  • 15
  • 38

3 Answers3

1

Change <%= to <%

<% @search_results.map do |sr| %>
  <%= sr.business_name %>
<% end %>
Kumar
  • 3,116
  • 2
  • 16
  • 24
1

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 %>
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
1

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

oreoluwa
  • 5,553
  • 2
  • 20
  • 27