I am very new to Rails, but have some Ruby understanding. How can I display the values of an array in a View in Rails?
Where should I define the array (model or controller)? Also, how can I reference the array and iterate between its members on a View?
You can just loop through it like:
But it’s much more idiomatic to use partials. Create a file that represents the element. The file should be in the same view directory with the other new/show/edit/etc view and should be named with an underscore. For example, if I had a list of foods as the array and I wanted to show the list on the index view, I would create a partial called “_food.html.erb” which would contain the markup for a given food:
Then in the index.html.erb, I would render all the foods like so:
Rails will look for the partial by default and render one for each element in the array.