[RUBY] Summary of initial work when creating an app with Rails


Since it may be set like every time when creating an application with rails, I will summarize the initial settings. The work is as follows.

・ App creation ・ Database creation ・ Introduction of gem ・ Introduction of Haml ・ Table creation ・ Create controller and view ・ Introduction of jquery

App creation

First, create an app by entering commands from the terminal.

I think it's a good idea to specify the version of rails that you are familiar with. (This time version 5.2.3, database uses myspl) Do the following in the directory where you want to save your app: (This time, the app name is sample)


$ rails _5.2.3_ new sample -d mysql

Move to the directory of the created application.


$ cd sample

Database creation

Create a database.


$ rails db:create

A database for development and test should be created. (Check with an app such as Sequel Pro)

gem introduction

Put the gem that you think you will use for development first. I think the following four can be included for the time being. Add the following to your Gemfile.


group :development, :test do
  gem 'pry-rails'

gem 'haml-rails'
gem 'font-awesome-sass'
gem 'jquery-rails'

I will explain each one pry-rails: To stop processing on the controller and check the contents of params haml: Because haml is used (easier to write than erb) font-awesome-sass: to use the font-awesome icon jquery-rails: to use jquery

Install after adding


$ bundle install

Introducing Haml

I have added haml to gem, but I will change the erb file that was originally created when creating the application to haml. Just run the following command:


$ rails haml:erb2haml

Running the command will convert the existing erb file to a haml file. After executing the command, you will be asked Would you like to delete the original .erb files ?, so enter y. (The question is, is it okay to delete the original erb file? Let's answer with good → y, no → n)

Create table

Create a table in the database. This time I will create a posts table. (I think that it will be after designing the DB when actually creating the application, but please think that it is for easy confirmation of the flow)

■ First, create a model.


$ rails g model post

A migration file and a model file should be created like the image below. 6b09d101921453a7b9b21fad5bec9fe1.png

■ Then edit the migration file. Describes the information of the column to be created. This time, the column name is content, the data type is string, and null is restricted. Let's add it to the migration file! (The migration file is in db / migrate)


class CreatePosts < ActiveRecord::Migration[5.2]
  def change
    create_table :posts do |t|
      t.string :content, null: false

■ Let's perform migration Migrate after writing the migration file.


$ rails db:migrate

It is OK if the table is created as shown in the image below. f719a52b32f13a6df5d62f5569dd5d00.png

Controller, view creation

■ First, create a controller. (Without this, it won't become an app ...) This time we will create a posts controller.


$ rails g controller posts

I think that various files will be created when you execute the command, but for the time being, it is OK if you pay attention only to posts_controller.rb. (A view folder for the posts controller is also created here) 9be2237cae0ffacc53580744ec6464be.png

■ Controller editing Let's write the action on the controller. (File is in app / controllers)


class PostsController < ApplicationController
  def index

For the time being, only the index action is described. This time, I will omit the description of the contents such as instance variables.

■ Routing settings Next, let's set the routing to use the controller. (File is in config)


Rails.application.routes.draw do
  root "posts#index"
  resources :posts, only: :index

The index action of the posts controller can be used. Also, change the root path. (Without this, when you access the root path, the default page prepared by rails will be displayed.)

■ Routing confirmation Let's check if the routing is set up correctly


$ rails routes

It is OK if the index action of the posts controller is set as shown in the image below. e901ca940d1239356297f31fbf5a6ea5.png

■ View file creation After setting the routing, prepare the view file. (Without this, even if you skip the request, you will get an error because there is no file)

Create index.html.haml in app / views / posts. Let's write something so that the contents are easy to understand.


%h1 index page

■ Check with a browser After creating the view file, let's actually check with the browser whether it is working. Start a local server.


$ rails s

Enter localhost: 3000 / in the URL with a browser such as Chrome to access it. It is OK if it is displayed according to the prepared view file as shown in the image below.


Introduced jquery

We will introduce jquery. Let's add require jquery to application.js.


//= require jquery
//= require rails-ujs
//= require activestorage
//= require_tree .

--- Digression --- By the way, if you add Ajax manually using jquery, it may interfere with turbolinks, so in my case I have deleted turbolinks. Comment out the description of turbolinks in Gemfile Removed turbolinks description in application.html file Removed turbolinks description in application.js file

Finally, let's check if jquery works properly in the browser. Anything is fine, so write the following in the js file and take a look at the browser console! You can check the console screen using the chrome verification tool. (This time it is for confirmation, so write it in application.js)



If OK is displayed as shown in the image below, it is working properly. 5d3352fcaeb81a05709680d955d6dccc.png

This is the end of all the work when creating an application. I would appreciate it if you could point out any mistakes.

Recommended Posts

Summary of initial work when creating an app with Rails
Rough procedure verbalized output when creating an app with Rails
How to specify db when creating an app with rails
Downgrade an existing app created with rails 5.2.4 to 5.1.6
Note: Cheat sheet when creating Rails Vue app
The story of making an electronic New Year's card app with Vue.js + Rails
How to push an app developed with Rails to Github
[Rails] Initial setting of user-created login with devise, devise_token_auth
How to get started with creating a Rails app
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
Creating an EC site with Rails5 ①-App configuration, various gem preparation, Model / Routing creation-
I want to push an app made with Rails 6 to GitHub
What to do when you launch an application with rails
Be careful of initialization timing when using MessageEncryptor with Rails 5.2 / 6.0
Building an environment for creating apps with Rails and Vue
List of copy and paste collections that are useful when creating apps with Ruby on Rails
The basics of the process of making a call with an Android app
The story of the first Rails app refactored with a self-made helper
Naming convention when creating a new controller or model with rails
Do not add @GeneratedValue unnecessarily when creating an Entity with JPA
Summary of problems and countermeasures when operating IE with WebDriver of Selenium2
[Beginner] When rails s doesn't work
Summary of rails validation (for myself)
Create an app with Spring Boot 2
[JVM] Summary when stumbling with jstat
[Rails] Creating a new project with rails new
[Rails] Summary of complicated routing configurations
About creating an application with springboot
Creating a timer app with a muddy
Summary of devise controller initial state
[Rails] Initial data creation with seed
Create an app with Spring Boot
Precautions when creating PostgreSQL with docker-compose
Track Rails app errors with Sentry
When PyCall doesn't work with PyCall :: PythonNotFound
Initial data input with [Rails] seed_fu!
Summary of means when you want to communicate with HTTP on Android
Create a LINEnews-style tech news summary app with Rails x LineBot! [Part 1]
A collection of methods often used when manipulating time with TimeWithZone of Rails
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Personal memo] Summary of stumbling blocks when deploying Rails apps to AWS
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Creating an inquiry form
[Rails6] Create a new app with Rails [Beginner]
SCSS doesn't work when deploying Rails6 AWS
Introduced Vue.js to an existing Rails app
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
When creating an app with the new command in Ruby on Rails bundler: failed to load command: spring (/ Users/user name/directory name to create the app/app name/vendor/bundle/ruby ​​/ 2.6.0/bin/spring )
Error when building infrastructure with aws app
Rails Basics of creating a new application
[Rails 5] Create a new app with Rails [Beginner]
[AWS] Publish rails app with nginx + puma
Let's make an error screen with Rails
Introduced Vuetify to an existing Rails app
Try creating an iOS library with CocoaPods
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
Rails6 I want to make an array of values with a check box