jeudi 28 avril 2016

NoMethodError: undefined method 'clients' for nil:NilClass

I've run into the infamous NoMethodError which I can't seem to crack, even after browsing the number of SO posts.

Note: I am running on Cloud9 development environment

I'm making a productivity website where a user can have their clients, projects, etc.

I'm attempting to run the clients_display_test.rb test and am receiving the following terminal error.

Any help would be appreciated but I would ask, if you determine where my mistake is, please specify where my lack of technical understanding was :)

  1) Error:
ClientsDisplayTestTest#test_profile_display:
NoMethodError: undefined method `clients' for nil:NilClass
    app/controllers/clients_controller.rb:4:in `index'
    test/integration/clients_display_test_test.rb:11:in `block in <class:ClientsDisplayTestTest>'

48 runs, 127 assertions, 0 failures, 1 errors, 0 skips

clients_display_test.rb

require 'test_helper'

class ClientsDisplayTestTest < ActionDispatch::IntegrationTest
  include ApplicationHelper

  def setup
    @user = users(:Robert)
  end 

  test "profile display" do
    get '/clients'
    assert_template "clients/index"
    assert_select 'title', "Clients"
  end
end

Attached is some .rb files which hopefully could help:

clients_controller.rb

class ClientsController < ApplicationController

    def index
        @clients = current_user.clients.paginate(page: params[:page])
    end

    def show
    end
end

routes.rb

Rails.application.routes.draw do

  get 'password_resets/newedit'

  root              'static_pages#home'
  get 'about' =>    'static_pages#about'
  get 'signup' =>   'users#new'

  get 'login' =>    'sessions#new'
  #the page for new session
  post 'login' =>   'sessions#create'
  #creates a new session
  delete 'logout' =>'sessions#destroy'
  #deletes the session
  get '/clients' => 'clients#index'
  get '/clients/show' => 'clients#show'

  resources :users
  resources :clients 
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
end

Client Views:

index.html.erb

    <%= provide(:title, 'Clients') %>

<div class="clients-container container"> 
    <div class="row">
        <!-- Add pagination later for multiple folders over multiple pages --> 
    <% if current_user.clients.any? %>
        <%= render @clients %> 
        <%= will_paginate @clients %>
    <% end %>
    </div>
</div>

_client.html.erb

<div class="col-md-2 client-folder" style="margin: 10px" id="client - <%= client.id %>">
  <span class="clientName" ><%= client.client_name %></span> <br>
  <span class="contactName"><%= client.contact_name %></span>
</div>

Aucun commentaire:

Enregistrer un commentaire