I've a services model in my ruby on rails project but I'm unable to render it in pages#home view of my project I'm getting this undefined method `each' for nil:NilClass Although I've defined the variable in my Servicecontroller-
My ServicesController file-
class ServicesController < ApplicationController
def show
@services = Service.all
end
end
My show file-
<div class="container">
<h3>Services we offer-</h3>
<% @services.each do |service| %>
<h5><%= service.name %></h5>
<p><%= service.description %></p>
<% end %>
</div>
and the path of my show file of services view is this- app\views\services\_show.html.erb and I'm rendering it in my pages views app\views\pages\home.html.erb as <%= render 'services/show' %>, I'm still getting the error although I'm able to see all services in rails console using @services=Service.all command.
Can somebody help me out here?
@services = Service.allwhen the request is being processed byPagesController#homeyou need to actually set the instance variable in that method. Setting an instance variable in another controller does absolutely nothing. Andshowin rails shows a single record.indexcorresponds to a collection of records. - max