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: GitHub - 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?' } %> |