Listing & Editing Profile Pages -

I am relatively new to this and completely lost. 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 finally got it to work…but when I logged in as a user I received
several errors which can be noted in the accompanying jpgs.

The first jpg is the url that I am assuming the user will go to for
their profile page.

The second jpg (User-Index) is the error I get when I try to access the
index page as a non-admin.

The third jpg (User-Edit) is the error I get when I try to access the
Edit page for a user account as a non-admin.

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

<%= @user.username %>

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

Listing users

<% if notice %>

<%= notice %>

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

Ruby is case-sensitive. In your first jpeg, change the PRESENT? method
to present? (on line 16 of your UsersController file)

Your second jpeg is showing that you haven’t initialized your @user
object in the index action. You can initialize it in the UsersController
file, in the index method.

In your last jpeg, the error is the same error as the first jpeg.

Thanks, Christian. you’ve given me some things to consider, but
Present? is only in the error message, not in my code. Things seem to
work a little better if I comment out the authorize! line in my show and
edit methods, 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.

My error was that I overlooked adding authorization for User in my
CanCan ability.rb file. It fixes most of my errors, but still leaves my
with the problem of not being able to sign out or having the dynamic
elements of my html.erb pages work.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs