[Introduction] Try to create a Ruby on Rails application

Introduction

I just made my debut in Ruby on Rails at work the other day. When it comes to Rails, I had the impression that a Web service with screen display was created, but at work, the API was created with Rails. So, here I will try both creating a web service and an API.

environment

Windows 10 Home 64-bit

How Rails works

Rails works with a mechanism based on the MVC model. MVC comes from the acronyms "Model", "View", and "Controller", respectively, and often appears when developing applications with Rails.

Web service creation

Create a web screen that displays the "Hello World" that is customary for beginners.

1. 1. Create a new project

Execute the following command to create a new project for the web service.

Command line


rails new application name

* About folder structure

The folder structure under the application folder is in Rails Documents. You can check it in "What is Ruby on Rails> Folder structure> Folder list".

2. Creating a controller

Run the following command to create the controller. When creating a controller, move to the application folder created earlier and then create it.

Command line


cd application folder
rails generate controller controller name
* Here, the controller name is "hello_world".

The controller "hello_world_controller.rb" has been created under the "app / controllers" folder. The source code at the time of generation is as follows.

hello_world_controller.rb


class HelloWorldController < ApplicationController
end

3. 3. Creating an action

The methods defined in the controller are called actions. Edit the "hello_world_controller.rb" created earlier and try to define the "index" action.

hello_world_controller.rb


class HelloWorldController < ApplicationController
  def index
  end
end

* Batch creation of controller and action

The controller and action were created separately here, but it is also possible to create them all at once when creating the controller. To create all at once, execute the following command.

Command line


rails generate controller controller name action name 1 action name 2...

#Example
rails generate controller hoge index create

4. Add view

Under the "app / views / hello_world" folder, add the view "index.html.erb" corresponding to the index action.

Since it is assumed that "Hello World" will be displayed this time, describe as follows in "index.html.erb".

html:index.html.erb


<h1>Hello World</h1>

5. Add routing settings

Receive the request from the browser and add the setting to distribute to the controller. The file to be modified is "routes.rb" under the "config" folder.

routes.rb


Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  get '/helloworld', to: 'hello_world#index'
end

You can check the format in "Routing Definition" in Rails Document. This time, I described the get method corresponding to the GET request.

6. Start Rails server

Run the following command to start the Rails server.

Command line


rails s

7. Check the operation from the browser

Go to http: // localhost: 3000 / helloworld.

API creation

Create an API that returns "Hello World" in JSON format. The general flow is the same as for Web services.

1. 1. Create a new project

Execute the following command to create a new API project. Add --api to a normal command and create a project in API mode.

Command line


rails new application name--api

2. Creating controllers and actions

The execution command is the same as the Web service. This time, let's create a controller and an action at once. The controller name and action name are the same as for the Web service. You can see the effect of API mode when creating a project here. If you create a controller normally, files related to views such as view, helper, and asset will be created at the same time, but those files will not be created because the API does not require views.

3. 3. Edit controller

Add a process to return "Hello World" in JSON format.

hello_world_controller.rb


class HelloWorldController < ApplicationController
  def index
    render json: { data: "Hello World" }
  end
end

4. Change routing settings

Since the controller and action were created at once, the routing settings are automatically added, but I will change them.

Change before_routes.rb


Rails.application.routes.draw do
  get 'hello_world/index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

After change_routes.rb


Rails.application.routes.draw do
  get '/helloworld', to: 'hello_world#index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

5. Start Rails server

Run the following command to start the Rails server.

Command line


rails s -p 3001

6. Execute the curl command and check the operation

Execute the following command and confirm that JSON is returned.

Command line


curl --request GET --url http://localhost:3001/helloworld
"Hello World" is back in JSON safely!

Recommended Posts

[Introduction] Try to create a Ruby on Rails application
Preparing to create a Rails application
[Ruby on Rails] Try to create a service that makes local cats happy
(Ruby on Rails6) Create a function to edit the posted content
How to create a query using variables in GraphQL [Using Ruby on Rails]
(Ruby on Rails6) How to create models and tables
[Ruby on Rails] Read try (: [],: key)
How to use Ruby on Rails
Try to create a server-client app
Try "Introduction to Vert.x" on Gradle
<Dot installation> Introduction to Ruby on Rails5 Source code comparison
How to display a graph in Ruby on Rails (LazyHighChart)
Apply CSS to a specific View in Ruby on Rails
How to deploy a Rails application on AWS (article summary)
[Ruby on Rails] Introduction of initial data
Introduction to Ruby 2
Ruby on Rails --From environment construction to simple application development on WSL2
[Ruby on Rails] How to use CarrierWave
I want to add a browsing function with ruby on rails
Try deploying a Rails app on EC2-Part 1-
I get a Ruby version error when I try to start Rails.
Deploy to Heroku [Ruby on Rails] Beginner
Ruby on Rails controller create / delete command
Preparing to introduce jQuery to Ruby on Rails
Ruby on Rails application new creation command
[Ruby on Rails] How to use redirect_to
[rails] How to create a partial template
[Ruby on Rails] How to use kaminari
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Beginners create portfolio in Ruby on Rails
[Ruby on Rails] Use the resources method to automatically create routes.
[Ruby on Rails] Button to return to top
Steps to build a Ruby on Rails development environment with Vagrant
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Ruby on Rails] Create a pie chart for each column with Chartkick
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Volume of trying to create a Java Web application on Windows Server 2016
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] How to display error messages
[Ruby on Rails] Until the introduction of RSpec
[Rails] rails new to create a database with PostgreSQL
[Rails] How to create a graph using lazy_high_charts
How to add / remove Ruby on Rails columns
Try to create a bulletin board in Java
[Ruby on Rails] Select2 introduction memo for Webpacker
Introducing Rspec, a Ruby on Rails test framework
[Ruby on Rails] A memorandum of layout templates
Try deploying Rails application to EC2-Part 2 (Server construction)-
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
(Ruby on Rails6) Creating data in a table
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
I made a portfolio with Ruby On Rails
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Rails] Create an application
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.