The One Guide To Set Up A Rails Application

Generating a New Rails Application

In the terminal:

You’ll need to add the name of your application after “rails new”

Set Your Domain Model and Build Schema

By this point, you should have your domain models & attributes planned. Now it’s time to set that all up.

We all love shortcuts. Luckily for us, rails gives some really convenient generators to set up your database through a few lines in the terminal.

Create Migration Table:

In the terminal:

This action creates a file under your database library in the migrate folder. It will timestamp it and add the name of your file. This will create the class, set up the inheritance needed from ActiveRecord, and create a method with your attribute columns. As an added plus, timestamps are always added for you!

Add Column to migration table

Forgot to add a column, or need to update your table? This line is your friend.

In the terminal:

This will create a new add_column migration. All you’ll need to do is specify the name of the column and the data type.

Add controller:

In the terminal:

This line will create that controller file you’ll need under the Controllers folder. It will also create a class for you, pluralize your object name to follow restful convention, and inherit from ApplicationController. Add — no-test-framework to skip adding all the extra testing files you may not need.

Add model:

In the terminal:

This line will create a model file for you along with a named class inheriting from ApplicationRecord. Add — no-test-framework to skip adding all the extra testing files you may not need.

Add Migration, Model, Controller, View directory, and Resources.

If you’re building from from the ground up- this line will save you time and build pretty much everything you’ll need to follow MVC (model, view, controller) convention.

This line will create the following:

  1. Migration file: a file under your database library in the migrate folder. It will timestamp it and add the name of your file. This will create the class, set up the inheritance needed from ActiveRecord, and create a method with your attribute columns.
  2. Model file: a file for you along with a named class inheriting from ApplicationRecord.
  3. Controller file: a controller file you’ll need under the Controllers folder. It will also create a class for you, pluralize your object name to follow restful convention, and inherit from ApplicationController.
  4. View folder: a view folder for you to add your HTML.erb page files. This will only create the folder for you- without any files.
  5. Resources: routes with all seven routes in the routes.rb file.

The best part of using rails g resources is that everything in the MVC will connected and pluralized properly for you. No need to build each one individually, then need to debug for a missing “s” in your object naming conventions.

Build your pages

Building out your pages and logic will most likely require logic and code in your view file, controller file, routes, and model file. You’ll find yourself hopping between these files as you build out pages and features.

Let’s do a “view all” feature here for articles we want to display for our blog app.

  1. In the controllers/articles_controller.rb folder:

2. Create a new view file. index.html.erb

3. In config/routes.rb- be sure that the :index reference is there.

Let’s build a show page to show an article in our database. Data will be passed on to this page to link the article in reference. This will normally be in the form of a Primary Key ID from your database.

  1. In the controllers/articles_controller.rb folder:

2. Create a new view file. show.html.erb

3. In config/routes.rb- be sure that the :show reference is there.

You’ll be needing this page for a user to input and create objects to persist in your database. Here we will be adding a new article to our database through the new page. For this data input we will be using a from_for. We will also be using a concept called strong params for added security and to maintain data integrity.

  1. In the controllers/articles_controller.rb folder:

You’ll need to create two methods here. One for “new” and one for “create”. The strong params method will go under “private”.

2. Create a new view file. new.html.erb- here we will use the form_for.

3. In config/routes.rb- be sure that the :new & :create reference is there.

You’ll be needing this page for a user to edit & update objects in your database. Here we will be editing a saved article in our database through the edit page. For this edit input we will be also using a from_for. Just like the new & create, we will also be using a concept called strong params for added security and to maintain data integrity.

  1. In the controllers/articles_controller.rb folder:

You’ll need to create two methods here. One for “edit” and one for “update”. The strong params method will go under “private”.

2. Create a new view file. edit.html.erb

You’ll notice here that the form_for is the same for the new & create controller actions. Since the form_for is able to accept object data if it exists and submit a update to that object , or default to a new create form for a new object if no data is taken in- it is perfect and flexible enough to use for both actions.

3. In config/routes.rb- be sure that the :edit & :update reference is there.

I get techy on here. { firstName: “Kevin”, lastName: “Botero”, interests: [“tech”, “startups”] }