Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get this particular functionality within CanCan to work, you'll want to store an attribute on your Devise <code>User</code> model that indicates whether a particular user is an admin, or not.</p> <p>Start by creating an attribute on your <code>User</code> table called <code>admin</code>:</p> <pre><code># from command line rails generate migration AddAdminToUser user </code></pre> <p>In the migration, set the default value for the <code>admin</code> attribute to <code>false</code> (or <code>true</code>, depending on what behavior you want enacted by default):</p> <pre><code>class AddAdminToUser &lt; ActiveRecord::Migration def change add_column :users, :admin, :boolean, :default =&gt; true end end </code></pre> <p>Run the migration</p> <pre><code># from command line rake db:migrate </code></pre> <p>In your <code>User</code> model, create a convenience method to access the value of <code>admin</code>:</p> <pre><code># app/models/user.rb def admin? admin end </code></pre> <p>Then, in <code>ability.rb</code>, define the abilities you want to set for each user:</p> <pre><code>class Ability include CanCan::Ability def initialize(user) user ||= User.new # guest user (not logged in) if user.admin? # Admin user can :manage, :all else # Non-admin user can :read, :all end end end </code></pre> <p>Remember – by default, a new <code>User</code> is <em>not</em> an admin. You can always enable admin privileges on an existing user in the following manner:</p> <pre><code># from the Rails console user = User.find(some_number) user.update_attribute(:admin, true) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload