Home > Enterprise >  object in rails erb file not rendering properly
object in rails erb file not rendering properly

Time:07-28

I have an object like so

[
        {"name": "ryan", 
        "age": "12", 
        },
        
    ]

And code in my erb file like so:

             <div>
                <% @list.each do |student| %>
                    <%= student.each do |k,v|%>
                        <%=v%>
                    <% end %>
                <% end %>
            </div>

I would assume that only the value of the hash in the list would be what gets rendered, so just ryan and 12 being rendered in the template. However in the template, the values in the hash plus the whole list end up getting rendered, so something like

ryan, 12 {"name": "ryan", "age": "12", },

end up getting rendered. Im not sure why cause in the double for loop, I only have the v variable that I want rendered, not sure where the rest of the object is coming from. Am I missing something about how erb files work?

CodePudding user response:

Would suggest you to use student.each_value as you are not using the keys.

CodePudding user response:

Because you have <%= student.each (instead of just <% student.each) and so the value of the each is also being rendered (which is the full list you pass in).

  • Related