Reputation: 393
I'm trying to insert a object into a array, but instead of insert into array, it prints in the screen the memory addresses.
I have the following code in my controller:
@article = Article.new("test","test1")
@articles << @article #this line causes the prints
And my view have this code:
<%=
@articles.each do |a|
a.titulo + " / " + a.texto
end
%>
Upvotes: 0
Views: 1646
Reputation: 5732
This is likely expected behavior. If @articles
is indeed an array, this should work. Just check the array after the append and make sure the newly added element is there. If you are doing this from the console, then the printing you are seeing is probably console behavior and not <<
behavior.
Because of the comment below, I'm changing my answer to reflect the actual problem. You are trying to render the enumerator to the view, and not the articles. Your view should look like:
<% @articles.each |a| %>
<%= a.titulo + " / " + a.texto %>
<% end %>
Upvotes: 1
Reputation: 1121
Create @articles
as an Array
before adding to it with <<
.
E.g.,
class MyController < ...
def your_method
@articles ||= []
@article = Article.new("test","test1")
@articles << @article
end
...
end
(and/or in a before_
filter and/or...)
Upvotes: 1
Reputation: 37409
Your problem is with how you render the @articles
:
<%= @articles.each do |a| a.titulo + " / " + a.texto end %>
The return value of each
is the array itself. You want to render each element within the array, so you should do something like this:
<% @articles.each do |a| %>
<%= a.titulo + " / " + a.texto %>
<% end %>
Upvotes: 3
Reputation: 6942
You could force @articles to be an array like this:
@articles ||= []
Then add your @article like you were already doing
Upvotes: 1