Learn
Authorization
Editor Role I

Great! In the users table, we now have a column named role that we can use to assign different roles to users, such as “editor” or “admin”.

Instructions

1.

Let’s add a method to the User model that will help us use the role column in our application. Within the class User, beneath the has_secure_password method, type

def editor? self.role == 'editor' end

We can use this method to determine if a user has the role of editor.

2.

Open db/seeds.rb. We’ve added a user named Mateo with the role ‘editor’. Seed the database with this data

$ bundle exec rake db:seed
3.

We should now be able to use the editor? method to check whether a user has an editor role. First, start the Rails console by running

$ rails console

Then check whether Mateo is an editor

> mateo = User.find_by(email: '[email protected]') > mateo.editor?
Folder Icon

Sign up to start coding

Already have an account?