Tuesday, 27 August 2013

Rspec/Rails: Failure/Error: click_on 'John Huston' ActionView::Template::Error: undefined method `name' for nil:NilClass

Rspec/Rails: Failure/Error: click_on 'John Huston'
ActionView::Template::Error: undefined method `name' for nil:NilClass

I am writing a spec for my rails app but keep getting the following error.
when I run rspec.
Failure/Error: click_on 'John Huston'
ActionView::Template::Error:
undefined method `name' for nil:NilClass
Here is the context for the line in which the error occurs:
it 'should display all entries belonging to a project' do
proj = FactoryGirl.create(:project, name: 'John Huston')
now = Time.now
later = now +3600
entries = 5.times.map do
FactoryGirl.create(:entry, project_id: proj.id, :date => 9/10/13,
:type_of_work => 'chump', :description => 'chumpin',
:phase_id => 8, :status => 'Draft' , :on_off_site
=> 'off', :user_id => 1, :start_time => now,
:end_time => later)
end
other_proj = FactoryGirl.create(:project, name: 'Groucho Marx')
other_entries = 2.times.map do
FactoryGirl.create(:entry, project_id: other_proj.id, :date =>
9/10/13, :type_of_work => 'chump', :description => 'thumpin',
:phase_id => 8, :status => 'Draft' , :on_off_site
=> 'off', :user_id => 1, :start_time => now,
:end_time => later)
end
visit projects_path
save_and_open_page
click_on 'John Huston'
Here is my project model:
class Project < ActiveRecord::Base
attr_accessible :name, :phases_attributes
has_many :entries
has_many :phases
accepts_nested_attributes_for :phases, allow_destroy: true
validates_presence_of :name
default_scope { order('LOWER(name)') }
def entries_by_user(user)
entries.where(:user_id => user.id)
end
def total_hours(user)
entries_by_user(user).map(&:total_time_as_hours).reduce(:+) || 0
end
end
Here are my project controllers:
class ProjectsController < InheritedResources::Base
# GET /projects
# GET /projects.json
def index
@projects = Project.all
authorize! :read, @projects
end
# GET /projects/1
# GET /projects/1.json
def show
@project = Project.find(params[:id])
authorize! :read, @project
end
# GET /projects/new
def new
@project = Project.new
authorize! :create, @project
end
# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(params[:project])
authorize! :create, @project
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was
successfully created.' }
format.json { render action: 'show', status: :created, location:
@project }
else
format.html { render action: 'new' }
format.json { render json: @project.errors, status:
:unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
@project = Project.find(params[:id])
authorize! :edit, @project
respond_to do |format|
if @project.update_attributes(params[:project])
format.html { redirect_to @project, notice: 'Project was
successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @project.errors, status:
:unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end
end
Here is my project show template:
%p{id: 'notice'}
= notice
%p
%strong
Project Name:
= @project.name
%ul
Phases:
-if @project.phases
-@project.phases.each do |phase|
%li
= phase.name
%table.entries
%thead
%tr
%th Project
%th User
%th Created By
%th Date
%th Start
%th End
%th Type
%th Status
%th On/Off
%th Phase
%th Description
%th Edit
%th Delete
%tbody
- if current_user
- @project.entries_by_user(current_user).each do |entry|
%tr
%td= entry.project.name
%td= entry.user.email
%td= entry.created_by
%td= entry.date
%td= entry.start_time.to_s(:time)
%td= entry.end_time.to_s(:time)
%td= entry.type_of_work
%td= entry.status
%td= entry.on_off_site
%td= entry.phase.name
%td= entry.description
%td= link_to 'Edit', edit_project_entry_path(:entry=>entry,
:id=>entry.id, :project_id=>@project.id)
%td= link_to 'Delete', entry, method: :delete, data: { confirm:
'Are you sure?' }
= link_to 'New Entry', new_project_entry_path(@project)
= link_to 'Edit', edit_project_path(@project)
= link_to 'Back', projects_path
My first thought was that in the show method of my projects controller
@projects was somehow not being properly set, and therefore was nil and
then when it called @projects.name in the show view, it was getting the
nil error. However, I tried wrapping that call to @projects.name with a if
@project to make sure @project was getting set but I got the same error as
before.
Any ideas?
Thank you!

No comments:

Post a Comment