I'm having some problems accessing specific indexes when iterating through a for loop in my view.
The code below is working fine to access the first element, however, I'm getting errors when trying to use the (element) block in place of 0.
@word contains an array of hashes
<!-- loop through word elements -->
<% @word.each do |element| %>
<!-- display word -->
<h1> <%= @word[0]["word"] %> </h1>
<!-- display definition -->
<p> <%= @word[0]["text"] %> </p>
<% end %>
I have a similar loop in my model file which works perfect for returning the array of hashes.
# create an empty response array for loop below
response = []
search.each do |element|
# Get back the first hash containing word information
# Without .first returns an array of hashes with multiple definitions for single word
response << Word.get_definitions(element).first
end
# return array of hashes containing information for each word
return response
Any help is greatly appreciated.