Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTessa Davis
4,152 Pointsfinding a value in a hash?
I'm having trouble displaying a value.
When I call @guidelines the page displays
[#<Guideline id: 25, title: "Bananas", content: "test me", hospital: "My Hospital", created_at: "2013-02-04 04:06:22", updated_at: "2013-02-07 01:37:49", user_id: 10>]
I want it to display "My Hospital"
But when I call
@guidelines.hospital
I get 'undefined method `hospital' for #<Array:0x007f8a937a1e38>'
Have also tried
@guidelines.index(hospital)
and a few others but cannot seem to get it to work.
Can anyone help?
2 Answers
Jason Seifer
Treehouse Guest TeacherHey Tessa,
The @guidelines variable is an array. You'll need to iterate over the individual items in order to access it. Try something like this:
<% @guidelines.each do |guideline| %>
<%= guideline.hospital %>
<% end %>
Tessa Davis
4,152 PointsThanks for the reply Jason. I did manage to work this out - guideline.hospital is the same for each guideline on this page so I only wanted it printed out once. I did it as follows:
<% @guidelines.each_with_index do |guideline, index| %>
<% if index==0 %>
<%= guideline.hospital %>
<% end %>