Explanation of Ruby on rails for beginners ①

Introduction

This time is a commentary article on Ruby on rails.

I will explain in an easy-to-understand manner for beginners.

Keep it on.

This time, we will ask Takashi-kun to cooperate in writing the article.

What is Ruby on rails?

Ruby on rails is a type of web framework.

First, I will explain what a framework is in the first place.

What is a framework?

A frame is simply a framework.

Ask your wikipedia teacher what the framework means.

In programming, it is an abstract concept that attempts to give a specific function by the user selectively overwriting or specializing common code with a general function.

It's still a hard word, so I'll break it down and explain it.

I think it's okay to think of a framework as a framework that provides various functions.

Now let's look at a concrete example of a framework.

Bootstrap When you try to create a website, it's hard to write HTML and CSS steadily.

Naturally, I want to make it easier. How can I make it easier?

That's right, you can enjoy it if you pack something that someone has made.

However, if you just park your website, you will get angry and the variations you can make will be limited.

Takashi thought here.

Takashi-kun "By the way, the parts used for websites are limited. If there is a framework that makes it easy to put the header part and button part individually, isn't it easy to write? "

This is how Bootstrap was born.

Bootstrap is a CSS framework that provides a framework for each part of your website.

What is a web framework?

You can understand from the explanation so far.

A web framework is a framework for creating web apps.

Let's ask Takashi again.

Takashi-kun "It's terrible to make a web application. However, the functions used in the web application are limited. You can operate the database according to the URL entered by the user, create an HTML file and send it. You just need a login function. Someone can create this framework, and if you pack it, you can write it easily, right?"

This is how web frameworks and Ruby on rails were born.

Now, let's actually make an app with Ruby on rails.

Rails new

First, install Ruby and Ruby on rails on your local computer.

Various people have written articles about environment construction, so I will omit it.

In most cases, building an environment will not work, but please continue to investigate until you can. I support you.

First, go to the directory where you want to create the rails app and type the following command.

rails new qiita_project

Please name anything after new.

The file will be created as follows.

image.png

You have now created a framework for creating a web app.

Start the server (rails server)

Go to the created qiita_project.

cd qiita_project

You can now move to the created qiita_project.

Next, let's start the server. The following command.

rails server

Next, enter the following URL in the URL field of your browser such as chrome.

http://localhost:3000/

image.png

At this point, you have started the server.

Create controller (rails generate controller controller name)

You can create a controller with the following code.

We'll see what the controller does later.

rails generate controller home

Running the above code will create a file named home_controller.rb under app >> controllers.

image.png

Let's take a look at the contents of home_controller.

home_controller.rb


class HomeController < ApplicationController

end

In this way, I was able to create a controller.

Now, let's explain the controller.

What is a controller?

To understand the controller, you first need to understand the figure below. It is about the mechanism that rail works.

image.png

Let's look at them in order.

① The user sends a request to the server

image.png

First, the user on the left sends a request to the server. The various actions on the right are all server-side stories.

Sending a request is to request something from the server. For example, if you enter a URL, you will be requested (get request) to "send a file corresponding to this URL!", And if you try to log in with your ID and password, you will be asked to "send the data you sent." Use it to do something! ”Will be requested (post request).

② Decide which controller to use by routing

image.png

Routing processes URLs received from users.

For example, suppose you receive the URL home / top from a user.

The routing will then translate to a routing called home # top.

This means "use the top action of the home controller". Think of an action as "code that does something" that is set for each controller.

This time, for the sake of simplicity, I think that the user requested (get request) "Please send the file corresponding to this URL!".

Let's route the URL "home / top" from the user to the command to perform the top action of the home controller.

Add the following to the routes.rb file under the config directory.

routes.rb


Rails.application.routes.draw do
  get "home/top" => "home#top"
end

Search the view file with the controller

image.png

Suppose a user sends you a URL called home / top and it is routed to home # top.

In this case, you will be doing the top action on your home controller.

Let's add a top action to our home controller as follows.

home_controller.rb


class HomeController < ApplicationController
    def top
    end
end

When you write the code like this, the home controller will look for the home / top.html.erb file and return it to the user.

Create view file

Please add the top.html.erb file to the home directory under app >> views.

image.png

Edit the top.html.erb file as follows.

top.html.erb


<h1>Hello World</h1>

In this way, I was able to create a view file.

Let's actually see the response.

When the user types in the URL

Let's start the server again with the following code.

rails server

Type in the following URL in your web browser.

http://localhost:3000/home/top

Then, the following screen will be sent.

image.png

Let's take a quick look at what's happening.

When the URL http: // localhost: 3000 / home / top is sent, it will be converted to home # top by routing.

This will do the top action of the home controller.

Rails will then find the top.html.erb file in your home directory under the app >> views directory and send it to you.

At the end

It's getting longer, so this time it's up to here.

I hope you will read the next article as well.

Explanation of Ruby on rails for beginners ② ~ Creating links ~

Explanation of Ruby on rails for beginners ③ ~ Creating a database ~

Explanation of Ruby on rails for beginners ④ ~ How to use naming convention and form_Tag ~

Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~

Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~

Explanation of Ruby on rails for beginners ⑦ ~ Implementation of flash ~

Thank you for your hard work.

Recommended Posts

Explanation of Ruby on rails for beginners ①
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
[Ruby on Rails] About bundler (for beginners)
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Basic knowledge of Ruby on Rails
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
part of the syntax of ruby ​​on rails
Rails [For beginners] Implementation of comment function
[Ruby on Rails] Japanese notation of errors
[Ruby on rails] Implementation of like function
Beginners create portfolio in Ruby on Rails
Ruby on Rails for beginners! !! Post list / detailed display function summary
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Scraping for beginners (Ruby)
Validation settings for Ruby on Rails login function
Implementation of Ruby on Rails login function (Session)
[Ruby on Rails] Until the introduction of RSpec
Recommendation of Service class in Ruby on Rails
[Ruby on Rails] Select2 introduction memo for Webpacker
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby on Rails] A memorandum of layout templates
[Rails] Procedure for linking databases with Ruby On Rails
[Ruby on Rails] Individual display of error messages
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Ruby] Explanation for beginners of iterative processing with subscripts such as each_with_index!
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Implementation of Ruby on Rails login function (devise edition)
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
[Ruby on Rails] Implementation of tagging function/tag filtering function
Ruby on rails learning record-2020.10.07 ②
(For beginners) [Rails] Install Devise
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Try using the query attribute of Ruby on Rails
[For beginners] Explanation of classes, instances, and statics in Java
Definitely useful! Debug code for development in Ruby on Rails
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
(Ruby on Rails6) Display of the database that got the id of the database
Delete all the contents of the list page [Ruby on Rails]