0

Suppose my array looks like this:

People => [ { Person => Jack, Age => 25 }, { Person => Frank, Age => 45 }, { Person => Molly, Age => 30 } ] 

I thought that iteration code would be in the controller since it is handling data. My controller code looks like this:

class People < ApplicationController def list people.each do |hash| @person = hash[person] @age = hash[age] end end end 

My view code looks like:

<h1>People</h1> <p> Person: <%= @person %> <br/> Age: <%= @age %> <br/> <br/> </p> 

When I load the page, rather than seeing three blocks of output, one for each person, I only see one block for Molly: the last iteration.

I'm thinking I need to put the iteration code in the view rather than the controller. Is this correct?

2 Answers 2

4

Yes, what happens is that when you set an instance variable ie. @person in a controller, it automatically gets thrown to the view. In this case, your code is overwriting @person with the last person that is available.

As you mentioned, put the loop in the view instead like so:

<h1>People</h1> <% @people.each do |person| %> <p> Age: <%= person.age %> <br/> <br/> </p> <% end %> 

(this is assuming you set an instance variable called @people, eg. @people = Person.all)

Sign up to request clarification or add additional context in comments.

3 Comments

Using your input, I was able to make this work. However, when I attempted to use the person.age format it failed with a NoMethodError for .age. Instead, I used person[Age] which presented the correct output. Do you know the reason for this?
yes, so I assumed that your collection (ie. @people) was a collection of ActiveRecord database objects (in which case you would be able to call person.age) - in your case since you have a nested hash you have to use the hash accessor (ie. person["age"]). I wonder why you have a nested hash there though instead of an ActiveRecord relation? anyhow :) if this is fixed, please mark accepted
Right now I'm getting the data from an outside source via separate API. It returns a hash which contains bare key=>value pairs and the array of hashes which I'm working with. Eventually, the API connection will only be used to compare the external data to that stored in the local database for discrepancy rectification. All actual manipulation will take place on local data.
0

Every time you loop through you are resetting the @person, @age instance variables. That is why you are left with only the last person. Assuming the person information is stored in your database, you should load all people with:

#controller @people=Person.all #then in the view <h1>People</h1> <%@people.each do |person|%> <p> Person: <%=person.person %> <br/> Age: <%= person.age %> <br/> <br/> </p> <%end%> 

2 Comments

Using your input, I was able to make this work. However, when I attempted to use the person.person and person.age format it failed with a NoMethodError for .person. Presumably, it would have done the same with .age had it reached it. Instead, I used person[Person] and person[Age]. Both presented the correct output. Do you know the reason for this?
@kempchee assumed (not unreasonably) that person was an ActiveRecord object so that attribute method person.person would work. But person is just a hash.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.