[RUBY] When a beginner makes a personal app with rails, the procedure to bring it to the starting point anyway.

Introduction

This time, I am writing an article assuming myself in the past who is thinking of making a web application for the first time using rails. After learning all the things, I understood "dots" to some extent about MVC, controllers, how to write methods, etc., but when I was working on "lines" called application production, my head was blank. I want to try various things, but I have to bring it to a working state for the time being. ** Anyway, it is a procedure to connect the routes and bring it to the point where the characters are displayed on the browser. ** ** From there, it is assumed that you will try and error what you want to do. You can do the same thing with scaffold, but I wanted to understand the flow and do it myself, so I'll explain it from the beginning.

version

ruby 2.6.5 rails 6.0.3.2

Target

** When I hit localhost: 3000, "Hello new world!" Is displayed. ** **

Rough flow

  1. Decide what kind of app you want. ↓
  2. Creating a template ↓
  3. Development environment settings ↓
  4. Model setting / migration ↓
  5. Routing ↓
  6. Controller settings ↓
  7. Preparation of view

1 to 3 are tasks that can be completed only once when creating an application. (With exceptions) Images 4 to 7 are repeated as a set for each function. It does not have to be in this order.

procedure

1. Decide what kind of app you want.

Decide the purpose, name, function, page design, and DB design. I think it's difficult to design perfectly from the beginning, but I'll make it as clear as possible.

2. Creating a template

In the creation directory

terminal


rails _6.0.0_ new sample-app -d mysql

--Specify the rails version with _6.0_0_. --sample is the app name --You can specify the database with -d.

3. Initial setting setting

config/database.yml


  encoding: utf8

Character code setting. At first, I think it is ʻencoding: utf8mb4, so change it. If it is not ʻutf8, an error may occur, so it is safe to change.

terminal


bundle install

If you have decided exactly what kind of GEM to put in step 1, it is better to put in GEM first.

terminal


rails db:create

Database creation. If you do not do this, the local server will not start.

4. Model setting / migration

You can reach your goal without doing step 4 this time.

terminal


rails g model book title:string author:string

This time, let's say you want to create a Book model with columns called title and author. Make the model name singular.

db/migrate/20201005134606_create_books.rb


class CreateBooks < ActiveRecord::Migration[6.0]
  def change
    create_table :books do |t|
      t.string :title
      t.string :author

      t.timestamps
    end
  end
end

The numbers in 20200919134606_create_books will be different.

terminal


rails db:migrate

This command is for migration.

5. Routing

config/routes.rb


Rails.application.routes.draw do
  root to: 'books#index'
  resources :post, only: :index
end

The index of Books is set to the root (the page that is displayed by default when accessed. The top page). Upon access, the Books controller's index action will be called and views / books / index.html.erb will be displayed in the browser.

terminal


rails routes

With this command, you can check whether the described route has been established.

6. Create controller

terminal


rails g controller books index

Create a Books controller. This command also creates a view at the same time. Make the controller name plural.

app/controllers/books_controller.rb


class BooksController < ApplicationController
  def index
  end
end

7. Preparation of view

html:app/views/books/index.html.erb


<h2>Hello new world!</h2>

Write a description and start a local server.

terminal


rails s

Let's access [localhost: 3000](http: // localhost: 3000 /) on the browser. If it is displayed without error, it is successful. If you get an error, you have to read the contents carefully and deal with it.

in conclusion

For the time being, once you have reached this point, repeat the work of describing 4 to 7 in detail and creating it.

Recommended Posts

When a beginner makes a personal app with rails, the procedure to bring it to the starting point anyway.
[Rails6] Create a new app with Rails [Beginner]
[Rails 5] Create a new app with Rails [Beginner]
[Rails] When transitioning to a page with link_to, move to the specified location on the page
[Beginner] What I learned when trying to introduce bootstrap to the rails6 app without using a CDN [Asset pipeline]
When importing CSV with Rails, it was really easy to use the nkf command
How to get started with creating a Rails app
[Rails] What to do when the view collapses when a message is displayed with the errors method
Rough procedure verbalized output when creating an app with Rails
[Rails] I tried to create a mini app with FullCalendar
How to specify db when creating an app with rails
What to do if the app is not created with the latest Rails version installed when rails new
The story of the first Rails app refactored with a self-made helper
[Beginner] Procedure to log in to the virtual environment built with Vagrant
[Swift] How to display when the quiz app answers correctly [Beginner]
Super beginner builds Rails6 + Postgresql environment with Docker to the end
How to interact with a server that does not crash the app
[Rails] How to apply the CSS used in the main app with Administrate
Make a login function with Rails anyway
What to do when is invalid because it does not start with a'-'
[Rails] Implementation procedure when public / private functions are added to the posting function
When I tried to use a Wacom tablet with ubuntu 20.04, I didn't recognize it.