asha February 2016

From the seeds file, default admin has to be created so that admin can login directly for the app and then add users.

The sign up page will not be present. Only the login page will be present. From the seeds file, default admin has to be created so that admin can login directly for the app and then add users. have users CRUD for admin. how to initiate this question

Answers


Hassan February 2016

Option 2 - Adding an admin attribute

The easiest way of supporting an admin role is to simply add an attribute that can be used to identify administrators.

$ rails generate migration add_admin_to_users admin:boolean

Add default: false to the line that adds the admin column to the table.

Your migration will now look like this:

class AddAdminToUsers < ActiveRecord::Migration def change add_column :users, :admin, :boolean, default: false end end

Next, execute the migration script:

$ rake db:migrate

Now you're able to identify administrators:

if current_user.admin? # do something end

If the page could potentially not have a current_user set then:

if current_user.try(:admin?) # do something end

With the above way if current_user were nil, then it would still work without raising an undefined method `admin?' for nil:NilClass exception.

The code below can be used to grant admin status to the current user.

current_user.update_attribute :admin, true and then you can simply create admin user through your seed.rb file or using rails console

Post Status

Asked in February 2016
Viewed 3,212 times
Voted 5
Answered 1 times

Search




Leave an answer