Reputation: 3323
I want to access a model in my application.html.erb file. For this I defined a before_filter in the application_controller.rb:
before_filter :populate_ages
protected
def populate_ages
@ages= Ages.all
end
In my application.html.erb I have the following:
<%= @ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
The names are rendered correctly but I get additional output which is the array @ages. It looks like this:
[#<ageid: 1, name: "Herren", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">, #<ageid: 2, name: "A-Jugend", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">, #<ageid: 3, name: "B-Jugend", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">] </ul>
I have this code in a navigation bar. So my navigation bar is extended by this additional text. Adding the loop in the body leads to the same output.
Upvotes: 1
Views: 55
Reputation: 118289
Just write as
<% @ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
@ages
is an Array
instance. Now Array#each
returns the receiver itself, when full iteration is completed. <%=.. %>
means you are telling execute the each m,method and print the result of the #each
method too, which is the problem, you have faced. Thus tell your template executed the method, don't print the result of it, and to do this correct syntax is <%.. %>
.
Upvotes: 1
Reputation: 10416
You don't want the = sign on the loop, change it to:
<% @ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
By having <%= @ages ...
you're telling rails to display the array
Upvotes: 1