Listing & Editing Profile Pages -

28 views
Skip to first unread message

Greg Bressler

unread,
Oct 8, 2015, 8:20:54 PM10/8/15
to rubyonra...@googlegroups.com
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:
<h1>hi</h1>

<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>

Attachments:
http://www.ruby-forum.com/attachment/11128/nonadmin-user-page.jpg
http://www.ruby-forum.com/attachment/11129/User-Index.jpg
http://www.ruby-forum.com/attachment/11130/User-Edit.jpg


--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Oct 9, 2015, 3:42:36 AM10/9/15
to Ruby on Rails: Talk
On 9 October 2015 at 01:20, Greg Bressler <li...@ruby-forum.com> wrote:
> 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.

Please don't post images, copy/paste the error here from the server
terminal window (which is often more informative than the browser
display).

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

Where did you get that code for application_controller? It is not
correct. Go back to wherever you got it from and check it carefully.
I imagine PRESENT? should be present?

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

As the error says, @user is nil.

>
> 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.

This is the same error as the first one.

Colin

Spencer Christiansen

unread,
Oct 9, 2015, 11:40:39 PM10/9/15
to rubyonra...@googlegroups.com
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.

Greg Bressler

unread,
Oct 10, 2015, 6:17:44 PM10/10/15
to rubyonra...@googlegroups.com
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.

Greg Bressler

unread,
Oct 10, 2015, 9:00:03 PM10/10/15
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Oct 11, 2015, 3:48:39 AM10/11/15
to Ruby on Rails: Talk
On 10 October 2015 at 23:16, Greg Bressler <li...@ruby-forum.com> wrote:
> Thanks, Christian. you've given me some things to consider, but
> Present? is only in the error message, not in my code.

Have a look at line 16 in
https://github.com/GBressler/esl-site/blob/master/app/controllers/application_controller.rb

Colin
Reply all
Reply to author
Forward
0 new messages