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
rake db:migrate
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:
u1.toggle(:admin)
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_out
that sends visits to theproducts
controller actionsold_out
. - Create a controller method
sold_out
in the products controller, and set the instance variable@title
in it to your desired title. - Create another instance variable
@products
and assign it all sold out products.
Add this line to routes.rb to create the route:
End of Free Content Preview. Please Sign in or Sign up to buy premium content.