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.
Ruby on rails is a type of web framework.
First, I will explain what a framework is in the first place.
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.
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.
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.
You have now created a framework
for creating a web app.
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/
At this point, you have started the server.
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.
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.
To understand the controller, you first need to understand the figure below. It is about the mechanism that rail works.
Let's look at them in order.
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).
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
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.
Please add the top.html.erb file to the home directory under app >> views.
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.
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.
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.
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