Admin and Admin Page
Premium Content - Free Preview
Let's create an Admin user and code our own page for admins.
Creating an Admin
The simplest way to create an admin is to add a boolean admin column to users. Go ahead and do that.
Enter the following two commands in your terminal:
rails generate migration AddAdminToUsers admin:boolean
The admin property can now be set from the rails console. Go ahead and set your user account to be an admin.
Start the Rails console and retrieve the user you want to modify (such as
u1 = User.first). You can use the
toggle command to toggle a property and save the change to the database:
Custom Admin Page
Let's create a page for admins to view sold out products. Try to do all the following tasks without looking at the solution:
- Create a route
sold_outthat sends visits to the
- Create a controller method
sold_outin the products controller, and set the instance variable
@titlein it to your desired title.
- Create another instance variable
@productsand assign it all sold out products.
Add this line to routes.rb to create the route: