I’m adding a second index file for a table called wostatuses.
The first index (index.html.erb) works fine.
But, the 2nd index (index2.html.erb) gets “The error occurred while evaluating nil.each” on the line:
<% @wostatuses.each do |wostatus| %>
The controller has:
class WostatusesController < ApplicationController
# GET /wostatuses
# GET /wostatuses.json
def index
@wostatuses = Wostatus.order("position")
respond_to do |format|
format.html # index.html.erb
format.json { render json: @wostatuses }
end
end
index.html.erb and index2.html.erb have:
<% @wostatuses.each do |wostatus| %>
In routes, I have:
get "wostatuses/index"
get "wostatuses/index2"
Thanks for the help!!
UPDATE –
<% Wostatus.find_each do |wostatus| %> worked!!!!
But, I need the records ordered by wostatus.position. How do I do that?
You have to make an action for index2 in your controller with the same code of index1. You can also make a method with the common functionality and use the method in both actions in order to write re-utilizable code.