Reputation: 145
I have a rails app that had a few views working. I made some changes that I cannot revert easily (I know, big mistake, mainly to the views though). I believe I am following Rails folder structure conventions correctly because the views WERE working. Now, when I try to see ANY view in the browser, I get the following error:
ActionController::RoutingError (undefined method `each' for nil:NilClass):
app/controllers/application_controller.rb:1:in `<top (required)>'
app/controllers/instance_controller.rb:4:in `<top (required)>'
I am not calling each in either of application_controller or instance_controller. instance_controller is the controller related to the view I am trying to render. If I use a different view, I will see a different controller in the error message instead of instance_controller. Application_controller is the parent of instance_controller. Even changing to an empty view with just some plain text or JSON will produce the same error. Is there some underlying Rails stuff happening that I have, perhaps overridden or something? Still really new to MVC and Rails in general. Even if you can't give me the exact answer, but perhaps a list of things to check over, anything helps.
application_controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
instance_controller:
require 'openssl'
require 'base64'
include HTTParty
class InstanceController < ApplicationController
def index
@tab = 'instance'
@server_list = Server.get_server_list()
@instances = ::ServerList.get_instances_on_all_servers
end
def self.update_credentials
Rails.logger.info(params.to_s)
in__hash = {
<some parameters>
}
response = put(
<some_path_I_can't_post>
)
end
end
EDIT: Additional info requested, config/routes.rb:
Rails.application.routes.draw do
resources :server
resources :instance
end
Output from running rake routes :
Prefix Verb URI Pattern Controller#Action
server_index GET /server(.:format) server#index
POST /server(.:format) server#create
new_server GET /server/new(.:format) server#new
edit_server GET /server/:id/edit(.:format) server#edit
server GET /server/:id(.:format) server#show
PATCH /server/:id(.:format) server#update
PUT /server/:id(.:format) server#update
DELETE /server/:id(.:format) server#destroy
instance_index GET /instance(.:format) instance#index
POST /instance(.:format) instance#create
new_instance GET /instance/new(.:format) instance#new
edit_instance GET /instance/:id/edit(.:format) instance#edit
instance GET /instance/:id(.:format) instance#show
PATCH /instance/:id(.:format) instance#update
PUT /instance/:id(.:format) instance#update
DELETE /instance/:id(.:format) instance#destroy
And we have app/controllers/instance_controller.rb, and app/controllers/application_controller.rb for paths.
EDIT 2 (Solved): I have been using unicorn_rails to deploy my website. I am not sure why, but I think my unicorn server was timing out at every request. restarting the server process removed these errors.
Upvotes: 0
Views: 155
Reputation: 251
I am thinking that you are calling each
on either @server_list or @instances within the index view and possibly one of those instance variables is nil
.
If so, you should wrap your each
block in an if
statement like so:
<% if @instances %>
<% @instances.each do |instance| %>
.....
do your thing here...
.....
<% end %>
<% end %>
You can also call to_a
on a variable which will return an empty array instead of nil if the variable is nil.
<% @instances.to_a.each do |instance| %>
.....
do your thing here...
.....
<% end %>
Upvotes: 1