Rails Authorization With Pundit

Adam Yarger · 21K views · 2015 · 1:20:02

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when someone asks how to add roles or authorization to a Rails app, and several askers say they are following it step by step.

Recommended by 1 people across 1 different months, 2015 to 2017.

What it is

A feature-length screencast by Adam Yarger that builds a Rails app with authorization handled by the Pundit gem. It covers setting up policies and user roles and includes a live demo app and the GitHub source.

What people said

Recommends K M Rakibul Islam · 2015-08 · Stack Overflow · on “how to add roles to my users in rails app?”

While you can add roles to your application like the way you did. But, it would be a lot of work to handle all types of roles and their responsibilities. Also, There are several nice gems like pundit [LINK] and cancan [LINK] which can be used to handle the authorization for your application users. They're very easy to integrate with your Rails application and works nicely with authentication gem devise [LINK] which you are already using. Here is a nice video tutorial [this video] which shows Rails Authorization with Pundit.

Cites Mel · 2015-12 · Stack Overflow · on “Rails 4 with Pundit”

I am trying to make an app in Rails 4. I want to use Pundit for authorisations. I also use Devise for authentication and Rolify for role management. I have a user model and am making my first policy, following along with this tutorial: [this video] I have a users controller with: class UsersController < ApplicationController before_action :set_user, only: [:index, :show, :edit, :update, :destroy] def index if params[:approved] == "false" @users = User.find_all_by_approved(false) else @users = User.all end end # GET /users/:id.:format def show # authorize! :read, @user end # GET /users/:id/edit def edit # authorize! :update, @user end # PATCH/PUT /users/:id.:format def update # authorize! :update,

Cites Nate · 2016-11 · Stack Overflow · on “Rails 4 - Update User Role Permissions”

Not sure if you're still working on this or what you're exactly trying to accomplish, but a good way to go around handling user roles with pundit is working with devise to handle user profiles and create an admin account. The admin would be able to change a specific user's role to whatever role you have as an option (i.e. user, vip, moderator, editor, admin, etc.). The sky is the limit in how complex you want your app to be with multiple roles. The use of "enum" in your user model will help guide your role options: class User < ActiveRecord::Base enum role: [:user, :vip, :admin] after_initialize :set_default_role, :if => :new_record? def set_default_role self.role ||= :user end devise :datab

Cites RalphShnelvar · 2017-07 · Stack Overflow · on “How to trace a html button non-event? (Rails Authorization With Pundit)”

Day 1: I am following along closely on [this video] . I'm at 1:02:11 in the video. [LINK] When I click on a "Change Role" button, sometimes I make it through to the controller (def update) action and sometimes I don't. If I don't then re-clicking the button does not seem to help. Refreshing the page is almost guaranteed to make the button work. Using Firefox's debugger, [LINK] how can I tell that Firefox has triggered a button event? The html for each user row (a partial named _user.html.erb) is <td> <%= link_to user.email, user %> </td> <td> <%= form_for(user) do |f| %> <%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %> <td> <button><%= f.submit 'Change Role' %></button> <