2016-04-28 8 views
0

Я столкнулся с печально известным NoMethodError, который я не могу взломать, даже после просмотра количества сообщений SO.NoMethodError: undefined method 'clients' for nil: NilClass

Примечание: Я бег на среде разработки Cloud9

Я делаю сайт производительности, где пользователь может иметь свои клиенты, проекты и т.д.

Я пытаюсь запустить clients_display_test.rb, и я получаю следующую ошибку терминала.

Любая помощь будет оценена, но я бы спросить, если вы определить, где моя ошибка состоит в том, просьба указать, где мое отсутствие технических знаний было :)

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 

Прилагается файл с расширением .rb, который, мы надеемся, может помочь:

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 

Клиентские Просмотров:

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> 
+1

Вы используете Придумайте? – GoGoCarl

ответ

1

кажется, что ваш current_user равен нулю. Вы должны войти в свой пользователь в тесте перед посещением страницы, иначе метод current_user вернет нуль.

Если вы используете Завещание для проверки подлинности, то есть тест-хелперы доступны, больше об этом здесь:

https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)

Смежные вопросы