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

Introduction

This time is a continuation of the previous article.

Explanation of Ruby on rails for beginners ①

Keep it on.

Routing changes

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.

image.png

Creating a link

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. image.png

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.

Why clicking a link jumps to another 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.

image.png

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.

image.png

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.

Actually create a link

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") %>

image.png

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.

Difference between <% =%> and <%%>

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.

How to use link_to

Since 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 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.

Add action to 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.

image.png

At the end

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

Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ①
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
Ruby on Rails for beginners! !! Summary of new posting functions
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Ruby on Rails] About bundler (for beginners)
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
[Ruby on Rails] Creating an inquiry form
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
Ruby on Rails for beginners! !! Post list / detailed display function summary
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] How to avoid creating unnecessary routes for devise
[Ruby] Explanation for beginners of iterative processing with subscripts such as each_with_index!
Ruby on Rails DB Tips for creating methods to reduce the load
Validation settings for Ruby on Rails login function
[Ruby on Rails] Until the introduction of RSpec
Recommendation of Service class in Ruby on Rails
[Ruby on Rails] Select2 introduction memo for Webpacker
[Rails] Procedure for linking databases with Ruby On Rails
(Ruby on Rails6) Creating data in a table
[For beginners] Procedure for creating a controller using rails
[Ruby on Rails] Individual display of error messages
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Scraping for beginners (Ruby)
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Ruby on Rails] Asynchronous communication of posting function, ajax
Implementation of Ruby on Rails login function (devise edition)
Docker the development environment of Ruby on Rails project
[Ruby on Rails] Implementation of tagging function/tag filtering function
Try using the query attribute of Ruby on Rails
Portfolio creation Ruby on Rails
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 basic learning ①
[Ruby on Rails] about has_secure_password
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
[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]