I will be using:
- Ruby 2.4.0
- Rails 5 with API key enabled
1. Create a folder for application:
2. Create Gemset for application and install rails gem:
3. Initialize a new git repository and generate a blank Rails 5 application:
4. Rails installed:
5. Adding IDE settings folder to .gitignore and committing changes:
6. Now launch a server and check if it’s working (I am launching it on port 3080, but the default is 3000):
This application will be used as a service for ordering food from local restaurants. So we need to have a few database models:
- Dish (title, type, ingredients, description, price)
1. Scaffolding restaurant model
I will use built-in Rails scaffold generator and define fields, that have to be created in a database, providing field type. The default type is string, so it can be skipped. The model Dish will belong to model Restaurant and Restaurant can have many dishes. Also, I will use Enumerable on a dish type field.
We can ignore warnings in this case (rails code is not updated to the changes in Ruby 2.4.0).
And I used rails trick to tell, that model dish belongs to model Restaurant.
Now I have to tell that Restaurant has many dishes and set dishes type:
Enum will provide our application with automatically generated methods for dish type. And it will be saved in a database as an integer starting with 0 where each next number will correspond to a dish type. For example
0 for european,
1 for pan asian,
2 for wok etc.
2. Migrating Database, configuring controllers and creating test data
Now I have to migrate the database and create dummy data:
Now if I visit
http://localhost:3080/dishes I will see data, stored in DB, in JSON format:
But I want to see all dishes that restaurant have on its page and restaurant name on a dishes page, so I have to make some changes to corresponding controllers:
And I see updated info in JSON:
3. Setting up ActiveAdmin
First of all, I need to add required gems to Gemfile:
And install them:
After that, we have to update app/controllers/application_controller from
Now I will install ActiveAdmin. I won’t be using any authentication, so I skip them.
When I visit http://localhost:3080/admin I see ActiveAdmin dashboard:
4. Creating and configuring ActiveAdmin resources
Now I have to generate ActiveAdmin resources:
It will add sections to create, list, update and delete dishes and restaurants, but default views and forms don’t satisfy my needs, so I will update them.
First of all, I will update app/admin/restaurant.rb, so it shows restaurant dishes count on the index page and dishes list on the show page. And also I tell ActiveAdmin, which parameters can be changed:
After that, I will update dish edit form so I can choose dish type from the list. To do that, I have to create a form partial at app/views/admin/dishes/_form.html.erb:
And render it from app/admin/dish.rb:
Now I can create, read, update and destroy restaurants and dishes from application backend.
ActiveAdmin can be used if you need to implement backend for your database. It is flexible, customizable and can be easily integrated.
Got a project idea? Let’s talk business
Originally published at Master of Code Global.