0
votes

I've been running through M.Hart's Rails tutorial. In one of the sections you implement delete links on the user index page, for each user that isn't your own; providing you are an admin user. I also want to implement a delete link into each of the user's profile page. I put the delete link code into a partial and it is running fine on the index page and the individual user show pages yet I am still getting RSpec failures after adding the partial to the show page code. My code is below... btw - a master user is the same as an admin user.

users/show.html.erb

<section>
    <%= gravatar_for @user %>
    <h3><%= @user.name %></h3>
    <div id="profile_delete_link"><%= render 'shared/user_delete_link', user: @user %></div>
</section>

shared/user_delete_link (partial -> _user/delete/link.html.erb)

<% if !current_user?(user) && current_user.master? %>
    >> <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %>
<% end %>

user_pages_spec.rb -> code below are the specs that break if the partial (above) is added to the users/show code. when the partial is removed these specs go back to green

describe "profile page" do
let(:user) { FactoryGirl.create(:user, name: "foo bar") }

let!(:mp1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:mp2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }

before { visit user_path(user) }

it { should have_selector('h3',    text: user.name) } 
it { should have_selector('title', content: "F.Bar") }

describe "microposts" do
  it { should have_content(mp1.content) }
  it { should have_content(mp2.content) }
  it { should have_content(user.microposts.count) }
end

end

user.rb

# == Schema Information
#
# Table name: users
#
#  id              :integer          primary key
#  name            :string(255)
#  email           :string(255)
#  password_digest :string(255)
#  created_at      :datetime
#  updated_at      :datetime
#  master          :boolean
#

class User < ActiveRecord::Base
has_secure_password 

email_regex = /^[_+a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i

attr_accessible :name, :email, :password, :password_confirmation
has_many :microposts, dependent: :destroy

    validates :name, :presence => true, :length => { within: 2..40 }
    validates :email, :presence => true, :uniqueness => { case_sensitive: false }, :format  => { with: email_regex }
    validates :password, :presence  => true, :length => { within: 5..50 }
    validates :password_confirmation, :presence => true

def feed
    Micropost.where("user_id = ?", id)
end
end

user_controller.rb show and index actions as well as before filters

before_filter :authorize,     only: [:index, :edit, :update, :destroy]
before_filter :correct_user,  only: [:edit, :update]
before_filter :master_user,   only: [:destroy]

def index
  @users = User.all
end

def show
  @user = User.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
  @title = initials_of @user.name
end

If you would like me to post any code or add any more information please let me know :) thanks in advance.

Update: RSpec errors:

UserPages profile page microposts
 Failure/Error: before { visit user_path(user) }
 ActionView::Template::Error:
   undefined method `master?' for nil:NilClass
 # ./app/views/shared/_user_delete_link.html.erb:1:in `_app_views_shared__user_delete_link_html_erb__1484829579316662700_70204671481360'
 # ./app/views/users/show.html.erb:6:in `_app_views_users_show_html_erb___32313850444620306_70204671449340'
 # ./spec/features/user_pages_spec.rb:88:in `block (3 levels) in <top (required)>'

SessionsHelper:

module SessionsHelper
 ...........
    def current_user
        @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end
 ...........
end
1
Please post your Rspec failures. - mind.blank
Done :) the rspec error above applies to all 11 spec failures - Josh Leeb-du Toit
How do your tests log in, so current_user is set? - Dave Newton
yes, i'll add the SessionsHelper code which contains the current_user method which just returns the current_user - Josh Leeb-du Toit

1 Answers

0
votes

What this response is telling you is that when it calls master? on the current_user object, the current_user object is Nil, meaning it doesn't exist.

This means that the @current_user isn't currently defined and User.find is not finding the user either.

You are passing @user to your partial, so I think your conditional should be based around @user.

Hope this makes sense