mercredi 26 février 2020

undefined method `each' for nil:NilClass even when defined in controller

I'm getting undefined method `each' for nil:NilClass for my @store.each code when I have defined @stores in my controller. I did a basic @stores=Store.all so i'm not sure why it's nil. I have Stores populated in my db.

My controller

class PagesController < ApplicationController
  def index
    @attendances = Attendance.all
    @stores=Store.all
    @periods=Period.all
    @employees=Employee.all
    @attendances = @attendances.uniq { |p| [p.period_id, p.store_id] }
  end

  def call_action

     @attendances = Attendance.where(store_id: params[:store_id], period_id: params[:period_id]) 
     @attendances.update_all(status: "Approved")
     redirect_to root_path
  end

  def review
    @stores=Store.all
    @attendances=Attendance.where(:status =>"For Review")
    @periods=Period.all.sort_by { |obj| obj.period_end }.reverse!

  end
end

My routes

Rails.application.routes.draw do
  devise_for :users
  resources :attendances
  resources :periods
  resources :employees
  resources :stores


 get '/pages/call_action' => 'pages#call_action', :as => :call_action
 get '/pages/review' => 'pages#review', :as => :review
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root 'pages#index'
end

my view pages/review.html.erb

<% @stores.each do |store|%>
          <%= store.name %>      
<%end%>

Aucun commentaire:

Enregistrer un commentaire