Learn how to build an ecommerce site using the Ruby on Rails programming language, including how to install and utilize the Active Admin gem for back-end website management.
This exercise is excerpted from Noble Desktop’s past web development training materials. Noble Desktop now teaches JavaScript and the MERN Stack in our Full Stack Development Certificate. To learn current skills in web development, check out our coding bootcamps in NYC and live online.
Topics covered in this Ruby on Rails tutorial:
Installing the Active Admin gem, Logging in to Active Admin, Generating a resource
Exercise Overview
In this exercise, we will work on building an ecommerce site. In particular, we’ll be focusing on some new and useful gems that we can add on to our site. Our site is called “That Nutty Guy” and it sells gag gifts.
To avoid repetition, we’re fast-forwarding past some of the tasks we did building Flix. Unlike Flix, we’re not going to start this site completely from scratch, but with the designer’s HTML and CSS already incorporated, plus a basic working model with index
and show
routes up and running.
If you’d like a refresher, or you’re interested in seeing exactly how we got the site up and running from scratch, we’ve included that information in three bonus exercises (B1–B3), which start from the designer’s HTML comps and build a basic working Rails site out of them. This exercise starts where B3 leaves off.
Getting Started
We’ve provided the designer’s HTML mockup for our ecommerce website—let’s take a look. On the Desktop, navigate to Class Files > yourname-Rails Class > That Nutty Guy HTML.
-
From the That Nutty Guy HTML folder, open index.html in a browser.
You can see that we have a nice, clean, responsive design for our ecommerce site.
Click on Tinfoil Hat to see the product page design template.
-
Click the Cart at the top right to see the Cart template.
The design looks great; we just need to make it functional.
Open Terminal.
Type
cd
and then press the spacebar once but do not hit Return yet!Switch to the Finder.
Navigate to Desktop > Class Files > yourname-Rails Class.
Drag the yourname-Rails Class folder from the Finder into the Terminal window (this will enter the path in Terminal).
Switch to Terminal.
Press Return to change to that directory.
Run
git clone https://bitbucket.org/noble-desktop/nutty.git
to copy the That Nutty Guy git repository.Type
cd nutty
to enter the new directory.Type
git checkout B3
to bring the site up to the end of Bonus Exercise 3.Run
bundle
to install any necessary gems.Run
yarn install --check-files
to install JavaScript dependencies.-
Start up the server by typing:
rails s
In a browser, navigate to: localhost:3000
Check out the site. It’s a work in progress, to be sure. Our products are showing up, and we can click through each one of them to view its show page, but they’re all showing the same Tinfoil Hat image (terrifying). We also need to create tools for admins to go in and make changes. Leave the browser window open so we can periodically come back and reload the page to see changes.
-
For this exercise, we’ll be working with the nutty folder located in Desktop > Class Files > yourname-Rails Class > nutty.
We suggest opening the nutty folder in your code editor if it allows you to (like Sublime Text does).
-
In your code editor, open the following file:
nutty > db > migrate > 20190722203407_create_products.rbHere we have specified the product information.
-
In your code editor, open the following file:
nutty > app > controllers > products_controller.rbHere we have a products controller with two views:
index
(the main page) andshow
(the product detail page). -
In your code editor, open the following file:
nutty > db > migrate > 20190722215056_devise_create_customers.rbWe also have Devise authentication for our customers. We have a customers table, a migration created by the Devise module.
Go back to the browser where localhost:3000 should still be open.
At the top right, click the Sign In link. Here you can sign in or create an account by clicking Sign up.
-
In your code editor, open nutty > db > seeds.rb
This contains all of the product data used to seed our site from scratch.
Starting Up Active Admin
We need to get all our images working. Before we can do that, we need a way to manage products from the back-end of the site. For Flix, we simply created our own forms and simple admin authentication to manage each model. That’s fine for a simple site, but this time let’s try out a gem called Active Admin. It is meant to be a comprehensive, highly-adaptable back-end system for content management. Using it can save you a lot of time building model forms, not to mention giving you the ability to filter, sort, and search through your model objects. It also helps you with all the other administration needs a complex website has. If you want more info about this gem, go to activeadmin.info to read more. Highly recommended!
In your code editor, open nutty > Gemfile
-
Scroll to the bottom of the file and add:
# Use Active Admin for content management gem 'activeadmin'
NOTE: Whenever we add a gem, it’s a good idea to include a comment explaining what it’s for in case we forget or another developer needs to edit it.
-
Save the file.
Now that we’ve got this new gem, we need to run
bundle
to install it. Remember, we need to stop the server whenever we install a new gem. Switch to Terminal and stop the server by hitting Ctrl–C.
-
In Terminal, type:
bundle
Whenever you install a new gem, it’s a good idea to consult the documentation to see if there are any additional installation steps. In a browser, go to activeadmin.info and at the top, click on Documentation.
-
Find the third step for after running the bundle. Go ahead and copy it (Cmd–C):
rails generate active_admin:install
Switch to Terminal, paste (Cmd–V) the command, then hit Return.
-
Return to the browser and copy the next step:
rails db:migrate
In Terminal, paste the command then hit Return.
-
Active Admin adds its CSS to the asset pipeline, which is all well and good, but we don’t want it mixing with our site CSS and changing the display of some items. To ensure it only appears for Active Admin, we just need to move the file. We can do this from the Terminal easily enough. First we need to create some subdirectories:
mkdir -p vendor/assets/stylesheets
-
Next, let’s move the .scss file:
mv app/assets/stylesheets/active_admin.scss vendor/assets/stylesheets/
All done!
-
We need to create an admin user. Open a rails console by typing:
rails c
-
Inside of the Rails console, type the following command and press Enter:
AdminUser.create(email: 'admin@example.com', password: 'password')
Type
exit
and press Enter again to leave the Rails console.-
The next step is to start up our Rails server. In Terminal, type:
rails s
In your browser, go to http://localhost:3000/admin and log in with the the information you entered.
The first thing we should do after logging in to changing the super insecure login info we used. At the top of the Dashboard, in the gray bar, click Admin Users.
There should only be one user currently, admin@example.com. To the far right of it, click Edit.
-
Set the following:
Email: your email address Password: any password you like Click the Update Admin user button.
-
Log in again with your new info.
NOTE: If you want to add another admin, all you need to do is click on the New Admin User button at the top right of the Admin Users page.
Managing the Site with Active Admin
There’s still a bit we need to do in order to be able to manage our site with Active Admin. We need to make Active Admin aware of our product model by generating a resource.
-
In the browser, go back to: activeadmin.info/documentation.html
The last step on this page tells us what to do.
Go back to Terminal. We want to leave our server running, so open up a new tab to work in (hit Ctrl–C).
-
Generate an Active Admin resource for our product:
rails g active_admin:resource product
In the browser, go back to: localhost:3000/admin
-
Notice that now there is a Products link at the top. Click it.
All our products are there with all their info! There are all sorts of parameters we can use to sort the list, as well as a Filters section on the right where we can type in specific filters to find a product.
-
To the far right of each product, notice the options View, Edit, or Delete. Click View next to the first one.
All its info is displayed. At the bottom is a section where Admins can add comments that are only viewable by admins on the Admin page (they will not display on the public website).
Go ahead and add the following comment about this product:
Out of stock with the manufacturer as of 1/24.Click the Add Comment button.
At the top right, click the Edit Product button. As you can see, this area allows us to make any edits to the product info. (You’ll get an error if you actually try to save at this point. We’ll handle that in the next exercise.)
-
Hit Cancel.
You should be back on the Products page.
Notice that there are also bulk editing options here. Click the checkbox to the left of any product(s).
-
Above the items, click the Batch Actions button. You should see an option to Delete Selected (but don’t do it).
That’s it for now. It’s amazing what we got with Active Admin and just a few commands in Terminal!
Leave the two tabs open and the server running in Terminal so we can continue with them in the next exercise.