This time is a continuation of the previous article.
Explanation of Ruby on rails for beginners ①
Keep it on.
You can configure the routing as you like.
For example, let's make home # top
(the top action of the home controller) occur when you write / pocomaru
.
Change the routing as follows:
routes.rb
Rails.application.routes.draw do
get "pocomaru" => "home#top"
end
In this state, type the URL as shown below.
http://localhost:3000/pocomaru
This will replace the URL http: // localhost: 3000 / pocomaru
with the instruction home # top
(the top action of the home controller).
Routing is done for URLs after localhost: 3000.
When executed, the following screen is displayed.
Now let's create a link.
In the file top.html.erb, link to the test.html.erb file.
The file structure is as follows.
The contents of the test.html.erb file should look like this:
test.html.erb
<h1>test</h1>
Let's create a link to this file.
But what does it mean to click a link and send it to the user?
Think about what's happening once. See the figure below.
Clicking on a link to jump to another file is the story in the red box in the figure above.
The user sends the URL to the server, the controller looks for the view file according to the URL, does the processing described in the action, and then sends the view file to the user.
Requesting a view file from the server in this way is called sending a get request
.
Users send URLs to the server in a variety of ways. You can click on the letter with the link, or press the button that says Send
after typing in the ID.
After such a URL was sent, the server translated it by routing.
Routing specifies which action on which controller to take.
Also, these actions correspond to view files.
For example, when the home controller top action
is called by routing, the top.html.erb file in the home directory under app >> views is called.
In other words, if you think about why clicking a link will take you to another file, you can answer as follows.
When you click the link, the URL is translated by routing, which action of which controller is specified, and the views file corresponding to that action is sent to the user
.
There are two main ways to create Ruby on rails links.
Since it's a big deal, let's implement it in two ways.
The top.html.erb file has been rewritten as follows.
top.html.erb
<h1>Hello World</h1>
<a href="test">test by href</a>
<%= link_to("test by link_to", "test") %>
The way to write the link on the second line is a typical way to write an html link.
Notice how to write the third line.
If you enclose it in the form <% =%>
, this is Ruby code! It will be told as
. The code using link_to is Ruby code, not html code, so you need to enclose it like this.
By the way, even if you enclose it in <%%>
, it will tell you that it is Rails code.
I will explain whether to enclose it in <%%> or <% =%> and how to use link_to below.
Actually, erb in .html.erb is an abbreviation for ʻEmbedded RuBy`. In other words, it means that it is an html file with Ruby embedded.
In other words, you can embed a Ruby script inside this html.erb file.
<% =%> And <%%> will be used when writing ruby code in the html.erb file.
However, when embedding a ruby script in an html file, there are cases where it does not need to be displayed separately in the browser.
In such a case, it is necessary to use these two properly. Roughly remember as follows.
<% =%>
to display in the browser<%%>
if you don't want it to appear in your browserSince link_to is Ruby code, it must be enclosed in <%%>
or <% =%>
.
This time, I want to create a link that jumps somewhere when I click it, so of course I need to display it in the browser.
Enclose it in <% =%>
.
link_to takes character to be displayed in the browser
as the first argument and ʻURL` as the second argument.
This time, I decided to take the URL test
.
From here, I will explain how to send the test.html.erb file when the link is clicked by the user, that is, the URL is sent.
First, let's change the routing.
Change the routing as follows.
routes.rb
Rails.application.routes.draw do
get "pocomaru" => "home#top"
get "test" => "home#test"
end
I added a new code to convert to the routing home # top
when the URL test
is sent.
In other words, when the user clicks on the URL test, the top action of the home controller will be executed.
Next, let's add the top action to the home controller.
Let's add the test action to the home controller.
hoem_controller.rb
class HomeController < ApplicationController
def top
end
def test
end
end
By adding such code, when the user calls the routing home # test
, the test action of the home controller is performed and the view >> home >> test.html.erb file is sent to the user. You will be able to send it back.
That's all for this time.
Thank you for staying with us so far.
Please see the following articles if you like.
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 ~
Recommended Posts