Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Delete users as admin

I am following along with Michael Hartls tutorial and I am trying to add the destroy action in order to delete users as admin. However, the link is not displaying under users name. I am also using Devise, not sure if that matters. Any help is greatly appreciated. Thanks

Controller/users

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

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

_users.html

<li> <class= "round-image-50"><%= image_tag(current_user.avatar.url(:thumb)) %> <%= link_to user.name, user %> <% if current_user.admin? && !current_user?(user) %> <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %> <% end %> </li>

Routes

devise_for :admins devise_for :users

resources :posts resources :users

get "users/show"

get 'feed', to: 'posts#index', as: :feed get "about" => "pages#about" root "pages#home"

match 'users/:id' => 'users#show', via: :get Migration

class AddAdminToUsers < ActiveRecord::Migration def self.up add_column :users, :admin, :boolean, :default => false end

def self.down remove_column :users, :admin end end

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

Are you sure you are logged in as the user who is an admin? You can check that in the rails console (in your terminal enter 'rails c' and then 'User.all', find that user and see if his admin field says true).

Yep, I doubled checked. The first user is myself and the admin field is set to true.

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

OK, so you are logged in as user 1, who is also an admin. Which user are you trying to delete? I think this: "!current_user?(user)" prevents you from removing yourself (in case the user you were trying to remove was you).

Correct. According to the tutorial I should be able to delete any/all user except myself. I am trying to delete multiple test users in which I have created and now no longer need. For some reason the delete link is not populating under the users name on my 'all users' page.

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

I assume you reloaded the whole server and the view in the browser. Try removing this "&& !current_user?(user)" and see if it works. Then put it back and delete this instead: "current_user.admin? &&" and see if this works. One of those conditions may be implemented improperly, this way you can check which one and do something about it.

I removed both and no change for the removal of either. The delete link still does not populate either way. If I add the link to my index page for the users then the link shows up however, everyone can see it. So I tried adding <% if current_user?(user) %> as well but then I get a syntax error in UsersController#index.

/Users/Dena/Desktop/uscout/app/views/users/index.html.erb:17: syntax error, unexpected keyword_ensure, expecting keyword_end /Users/Dena/Desktop/uscout/app/views/users/index.html.erb:19: syntax error, unexpected end-of-input, expecting keyword_end

Rails.root: /Users/

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

Yeah, the 'if' statement requires you to put this somewhere: <% end %>

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

OK, I assume that when you delete the whole <% if current_user.admin? && !current_user?(user) %> as well as the <% end %>, the links show up?

When I delete the entire thing the link shows up if I put it on the index page however, if I leave it on the _user page which is where the tutorial says to put it it still doesn't show up. I'm lost!

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

OK, do you have a GitHub account? Can you publish your whole project in a repository, so that I could take a look at it and run it locally? I can't assist you any further until I see the internals of your app and I don't know which files I need to look at exactly :)

Ok, great! Thank you. I will push it up now.

I was able to get the link to display for the admin by placing it on the user Index page.

However, now when I try to delete a user I get an Unknown action.

The action 'destroy' could not be found for UsersController

Despite the fact that I have it in there.

Controller/users

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

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