0
votes

I'm kind of new to RoR and I'm trying to make a user's profile page. If the user is an admin, they can see all the users, as well as access and edit/update each user's profile page. I'm using Devise and Cancan for authorization.

Everything seems to work fine when I'm logged in as an admin, but when I am logged in as a normal user I get the following error for the profile page:

NoMethodError in UsersController#show Undefined method 'PRESENT?' for nil:NilClass

Things seem to work a little better if I comment out the authorize! line in my show and edit methods of the User Controller, but then the sign out seems to break as well as the dynamic elements of my profile page (including the admin pages). I am not sure how to proceed.

The project can be found at: https://github.com/GBressler/esl-site

Any help that could be proved on these issues would be greatly appreciated.

Here is my Users Controller:

class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]

  def index
    if current_user.id == 1
      @users = User.all
    else
      render 'profile_page'
    end
 end

def show
  render 'profile_page'
  authorize! :show, @user
  @user = User.find(params[:id])
 current_user.first_name
end

def update
end

def edit
  authorize! :edit, @user
end

def destroy
end

private

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


def user_params
  params.require(:user).permit(:id, :first_name, :last_name, :email,
  :username)
end
end

Here's the code for my yet-to-be developed Edit and Profile Page:

hi

<p><%= @user.username %></p>

Here's the code for the index page that the admin sees:

<h1>Listing users</h1>
<!-- START_HIGHLIGHT -->
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<!-- END_HIGHLIGHT -->

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Username</th>
      <th></th>
      <th></th>
   </tr>
  </thead>

 <tbody>
    <% @users.each do |user| %>
    <tr>
      <td><%= user.first_name %></td>
      <td><%= user.username %></td>
      <td><%= link_to 'Show', user %></td>
      <td><%= link_to 'Edit', edit_user_path(user) if can?(:edit,
      user)%></td>
      <td><%= link_to 'Destroy', user, method: :delete,
      data: { confirm: 'Are you sure?' } %></td>
     </tr>
    <% end %>
  </tbody>
</table>
1

1 Answers

0
votes

I overlooked adding authorization for User in my CanCan ability.rb file.