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.
ruby 2.6.5 rails 6.0.3.2
** When I hit localhost: 3000, "Hello new world!" Is displayed. ** **
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.
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.
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
.
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.
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.
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.
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
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.
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