vendredi 21 août 2015

Failed test on users_edit_test.rb, Chapter 9 Ruby on Rails Tutorial 3rd edition

I am consistently receiving three errors and I have no idea why. I'm following the tutorial to the best of my ability, and I have scoured through Chapter 9 for mistakes and I haven't seen any. Running bundle exec rake test I get the three following errors:

FAIL["test_unsuccessful_edit", UsersEditTest, 2015-08-22 09:09:06 +0900]
 test_unsuccessful_edit#UsersEditTest (1440202146.96s)
        expecting <"users/edit"> but rendering with <[]>
        test/integration/users_edit_test.rb:12:in `block in <class:UsersEditTest>'

 FAIL["test_successful_edit", UsersEditTest, 2015-08-22 09:09:06 +0900]
 test_successful_edit#UsersEditTest (1440202146.97s)
        expecting <"users/edit"> but rendering with <[]>
        test/integration/users_edit_test.rb:25:in `block in <class:UsersEditTest>'

 FAIL["test_successful_edit_with_friendly_forwarding", UsersEditTest, 2015-08-22 09:09:07 +0900]
 test_successful_edit_with_friendly_forwarding#UsersEditTest (1440202147.07s)
        Expected response to be a redirect to <http://ift.tt/1hzlYYb; but was a redirect to <http://ift.tt/19BVxMi;.
        Expected "http://ift.tt/1UZjosw" to be === "http://www.example.com/".
        test/integration/users_edit_test.rb:43:in `block in <class:UsersEditTest>'

As you can see, all of my errors can be found in test/integration/users_edit_test. Two of my tests fail in the same way, with this message: expecting <"users/edit"> but rendering with <[]>. Line 12 and 25 has the same code:

assert_template 'users/edit'

Line 43 has the following code:

assert_redirected_to edit_user_path(@user)

I have a _header.html.erb file with a link that is supposed to go to the user's edit page but it just redirects to the index. I have a file app/views/users/edit.html.erb so I don't know why it isn't showing up. Here is the code for the settings link:

<li><%= link_to "Settings", edit_user_path(current_user) %></li>

Here is my code for my users_controller.rb file. I suspect the problem is my correct_user method not firing like it is supposed to. (By the way, I have tried commenting out the code inside the "edit" method, but the problem still persists.)

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update]
  before_action :admin_user, only: :destroy

  def index
    @users = User.paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
    # debugger
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
        log_in @user
        flash[:success] = "Welcome to the Sample App!"
          redirect_to @user
    else
        render 'new'
    end
  end

  def edit
  #  @user = User.find(params[:id])
  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id].destroy)
    flash[:success] = "User deleted"
    redirect_to users_url
  end

  private

    def user_params
        params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    def correct_user
      @user = User.find(params[:id])
    #  redirect_to(root_url) unless @user == current_user
      redirect_to(root_url) unless @user == current_user?(@user)  
    end

    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end
end

In app/helpers/sesions_helper.rb I have two methods, current_user and current_user?

def current_user?(user)
  user == current_user
end

def current_user
    if(user_id = session[:user_id])
        @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
        user = User.find_by(id: user_id)
        if user && user.authenticated?(cookies[:remember_token])
            log_in user
            @current_user = user
        end
    end
end

Here is the config/routes file:

root 'static_pages#home'
  get 'help' => 'static_pages#help'
  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup' => 'users#new'
  get 'login' => 'sessions#new'
  post 'login' => 'sessions#create'
  delete 'logout' => 'sessions#destroy'
  resources :users

I suspect the answer to my issue is super easy, but I've no idea what is wrong.

Aucun commentaire:

Enregistrer un commentaire