Rails 6 | Hit the ground running

Tiago Ferreira
4 min readDec 7, 2019

--

As a cohort of Microverse, I was assigned to write a technical article on Rails. I had this idea of building an app to help me improve English vocabulary, so why don’t I merge both of them?

I will explain all the steps that I took to build this little but amazing app.

http://vocabiapp.herokuapp.com

The source code is on Github. The code used on this article is on the branch feature/first-milestone.

Setup

  • Install Ruby
  • Install Rails 6
  • Create fresh app $ rails new vocabiapp
  • Install jQuery and Bootstrap $ yarn add jquery popper.js bootstrap

Register the jQuery plugin on Webpack. Copy this file into config/webpack/environment.js

At this point if you start $ rails server the Rails welcome page should be displayed. Troubles? Click this link to get a free infinite 1-on-1 mentoring.

Layout

Rails is an MVC (Model, View, Controller) framework, the resulting webpage is built by a combination of views, usually reusable pieces.

Rails Guides — Layouts and Rendering

We’ll start by creating the main layout and the partials that compose it. Then a StaticController to render our home page.

The render method calls a partial, yield will be replaced by the view specified on controller. (application.html.erb)

Did you notice .erb and <%= %>? The views on Rails use ERB which allows us to embed ruby code inside HTML.

Just moved the code inside <head> tag on the default layout to this partial. (_head.html.erb)
Method link_to referencing root_path, we will create this route shortly. (_header.html.erb)
The creator, I heard this guy is awesome. (_footer.html.erb)

Static Pages Controller

Have you ever heard that the best programmers are the lazy ones!? Rails believe it, that’s why they have created a complete set of code-generator tools.

Rails Guides — Command Line

$ rails g controller StaticPages home

Created: controller (1), unit test (2), helper (3), view (4), SCSS(5). Let’s fill the view views/static_pages/home.html.erb.

home.html.erb

The basic life cycle is Router -> Controller#Action ->[Model]? -> View.

Controller

The call to root method will assign the root_path (app.com/) to the method (action) homeof StaticPagesController.

Open the app and our new view will be displayed.

Word model

The model reflects a database entity (table) and contains validations, associations, callbacks.

At this first milestone, we will have only the Word model.

To create or edit entities Rails uses migrations. Using the model generator command the migrations will also be created.

$ rails g model Word rank:integer pos:string level:integer en:string es:string pt:string

This command created the Word [model, fixtures, test] and the following migration to create the words table on the database.

Run the migration with $ rails db:migrate.

Database seed

Imagine the amount of work to create five thousand new words by hand? With Rails we can include initial app data with $ rails db:seed.

As seed data this app uses:

It will not be covered here. Copy the files (seeds.rb, translations_*.txt, words.html) into your db folder and run the seed command.

Rails Guides — Active Record Migrations

By default we are using SQLite3, check the contents of the table words. (db/development.sqlite3)

Game Controller

Lets create the GameController with an action called play and the view.

$ rails g controller Game play

Add the route to this action with params :pos (part of speech) and :level.

The play action should return all the words matching :pos and :level.

Copy the view file (play view code). Note the usage of jQuery alongside with bootstrap collapsible elements.

The app uses two SVG icons to feedback, paste them into app/assets/images.

We should avoid adding too much logic into the view, the GameHelper into your project.

Last step, game.scss and we the coding is done.

Start the server with $ rails server and open it

http://localhost:3000/nouns/1

Conclusion

With Rails you can do a lot with a little, we have created an MVP in no time using the stack.

Progress further by:

--

--