[RUBY] Rails Basics of creating a new application

Basics of creating a new application

This time I will summarize the basics when launching a new application with ruby on rails.

As a premise, the version of rails is 6.0.0 and the database uses a tool called MySQL.

Preparing a new app

#Move directory
% cd ~/Directory name where you want to create an app

#Create a new app(App name chochiku)、-Created by explicitly using MySQL with the d option
% rails _6.0.0_ new chochiku -d mysql

#Move to the created chochiku directory
% cd chochiku

By typing the above code in the terminal, a new application will be launched.

Creating a database

Use the command to create the database for your app, but before you do that, you need to make a few settings related to the database.

Database settings are described in database.yml.

Write encoding: utf8mb4 under default in database.yml encoding: change to utf8

Then execute the following command to create the database

% rails db:create

Creating a model

Rails applications require a model that interacts with the database.

#Create Expense model
% rails g model expense

Creating a table

Edit migration file

When you create the above model, a migration file will be created in a directory called db / migrate at the same time. Edit this migration file to determine the information to save in the table.

class CreateExpenses < ActiveRecord::Migration[6.0]
  def change
    create_table :expenses do |t|
      t.string :name
      t.integer :shuppi

For example, the code t.string: name is of type string and adds a column called name to the table.

Perform migration

Editing the migration file does not mean that you have made any changes to the table. You need to perform the migration there. You can execute the migration by executing the following command.

% rails db:migrate


With the above, we have created one model of the application and a table linked to that model. Actually, from now on, we will create an application by creating controllers, routings, and views linked to this model. That's all for the time being as the basis for creating a new application.

Recommended Posts

Rails Basics of creating a new application
[Rails] Creating a new project with rails new
[Rails] Launch a new project
rails new application launch command
[Rails] Introduction of devise Basics
Creating a batch of Liferay
[Rails] Creating a search box
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Preparing to create a Rails application
Create a new app in Rails
Rails basics
Naming convention when creating a new controller or model with rails
Creating a mixed conditional expression of Rails if statement and unless
Install Rails in the development environment and create a new application
Creating a new user with rails was angry with unknown attribute'password' for User.
[Rails6] Create a new app with Rails [Beginner]
Memorandum of new graduate SES [Java basics]
Ruby on Rails application new creation command
[Rails 5] Create a new app with Rails [Beginner]
Rails CSV basics
Rails: A brief summary of find, find_by, where
Rails Routing Basics
A summary of only Rails tutorial setup related
Rails database basics
[Rails] rails new to create a database with PostgreSQL
Basics of Ruby
[Rails] Implementation of retweet function in SNS application
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
(Ruby on Rails6) Creating data in a table
Rspec Basics [Rails]
[Rails] Creating a breadcrumb trail using Gem gretel
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
[For beginners] Procedure for creating a controller using rails
[Review] When creating a web application with Rails, syntax error, unexpected')', expecting => ...]}% ","% # {params [: content]}% "]) ...
A review of the code used by rails beginners
[Introduction] Try to create a Ruby on Rails application
Basics of HTML forms indispensable for creating web applications
One case of solving a migration error in Rails
A brief summary of Rails association options (foreign_key, primary_key)
Experience of passing Java Silver as a new graduate
[Rails] Volume that displays favorites and a list of favorites
Roughly the flow of web application development with Rails.
Explanation of Ruby on rails for beginners ② ~ Creating links ~
How to get started with creating a Rails app
About rails application server
[Rails] Introduction of PAY.JP
Rails Tutorial/Significance of Indexing
Ruby on Rails basics
Basics of try-with-resources statement
Creating a test case
[Rails] Create an application
I made a Japanese version of Rails / devise automatic email
Story of making a task management application with swing, java
Ruby on Rails for beginners! !! Summary of new posting functions
Summary of initial work when creating an app with Rails
A story packed with the basics of Spring Boot (solved)
Check the operation of two roles with a chat application
About regular execution of rake task of rails application on heroku
A note about the seed function of Ruby on Rails
Procedure for publishing an application using AWS (4) Creating a database