I spent a lot of time trying to solve this problem and did not find the final completed examples online, so I put everything lower, so any new RoR / Devise users, hopefully, will not have the same problems.
Assuming what Deviseis in the model User. Make sure your Cancancan is configured accordingly. Something like this:
models / ability.rb
class Ability
include CanCan::Ability
def initialize(user)
current_user ||= User.new
if current_user.admin
can :manage, :all
else
can :manage, User, id: user.id
end
end
end
Follow the instructions in here.
, , . - , :
class UsersController < ApplicationController
before_action :admin?, only: :index
def index
if params[:approved] == false
@users = User.where(approved: false)
else
@users = User.all
end
end
private
def admin?
redirect_to '/login' unless current_user.admin == true
end
end
( .erb not.haml, ) %td= link_to "Edit", edit_user_path(user) : <% = User.approved% >
<td>
<% if !User.approved %>
<%= link_to "Approve User", user_path(:id => user.id, "user[approved]" => true), :method => :patch, class: "btn btn-success" %>
<% else %>
<%= link_to "Unapprove User", user_path(:id => user.id, "user[approved]" => false), :method => :patch, class: "btn btn-danger" %>
<% end %>
</td>
, , , . , , , a) , ( link_to Users, RegistrationsController#update.
, - Registrations , ..
, . , !