[RUBY] (Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 3]

Premise

・ Rails tutorial is the 4th edition ・ This study is the 3rd lap (2nd lap after Chapter 9) ・ The author is a beginner who has done all of Progate.

Basic policy

・ If you read it, you will not understand it. ・ Search and summarize terms that you do not understand (at the bottom of the article, glossary). ・ Dive into what you do not understand. ・ Work on all exercises. ・ Do not copy chords as much as possible.

Chapter 3 where the main sample_app finally begins. From this chapter, I will add today's BGM to accompany my learning. Please for a change. Hitsujibungaku "It was a human"

[3.1 Setup Exercise]

When I install the gem file, I get a deficit error immediately, but if I bundle update and install it again, it will be solved. And there is another error when deploying to heroku. ** Precompiling assets failed ** is in red. Since it is precompiled, is it an error before compiling? Even if you follow the log and check the part where the error is thrown, ** NoMethodError: undefined method ʻaction_mailer'**? After all, I couldn't find out even after checking it, so I rewrote the gem file again and bundle updated it, and it was able to deploy successfully.

  1. Check if Bitbucket correctly renders the Markdown notation README (List 3.3) as HTML. → In my case, it was Github, but it was drawn correctly (image omitted).

  2. Access the root URL of your production environment (Heroku) and see if the deployment was successful. → hello, world! Was displayed.

[3.2.1 Static page generation exercise]

Memo 1: ** Column 3.1 How to restore ** The generated code and how to restore the migration are posted. Note 2: Four basic HTTP operations ⇨ GET, POST, PATCH, DELETE

  1. Create a controller called Foo and add bar and baz actions to it. → Generated with the following code.
$ rails g controller Foo bar baz
  1. Try removing the Foo controller and related actions using the technique introduced in column 3.1. → Delete with the code below.
$ rails destroy controller Foo bar baz

[3.3 Start with test]

The test that finally came. At first I didn't understand it, but now I can understand it a little. The point is to write and confirm the expected movement first, "This works, right ?? (Or, this doesn't work, right ??)". In order to write a test, you need to draw the blueprint of the app first (which may be obvious). If you write "later" in column 3.3, it's only for code whose behavioral specifications aren't settled or are likely to change again soon. Basically, it seems to be based on writing "first". You may want to read the Rails Guide Testing Guide for the details of the test. It's a huge amount, so I'll look at it one by one. Some types of assertions are also listed. I will summarize what appears in this tutorial in the glossary below the article. I think I can remember that better.

[3.4.2 Adding a title (Green) Exercise]

  1. Did you notice that the StaticPages controller test (Listing 3.24) had some iterations? Especially the basic title "Ruby on Rails Tutorial Sample App" has the same content for each test. I will. So I would like to solve this problem with a special method called setup (the method that runs just before each test runs). First, make sure that the test in Listing 3.30 turns green (Listing 3.30 uses the technique of instance variable and string expression expansion, which we touched on in 2.2.2, respectively, 4.4.5 and 4.2, respectively. I'll explain it in detail in .2, so don't worry if you don't understand it now). → Just enter as shown in Listing 3.30.

[3.4.3 Layout and embedded Ruby (Refactor) exercise]

  1. Create a Contact page in the sample application 16 (Hint: First, refer to Listing 3.15, and the page with the URL / static_pages / contact will have the title "Contact | Ruby on Rails Tutorial Sample App". Let's first create a test to see if it exists. Next, let's display the content in Listing 3.40 on the Contact page, just as we did when we created the About page in 3.3.3. ). → First, create a test. Of course the result is an error.

static_pages_controller_test.rb


  test "should get contact" do
    get static_pages_contact_url
    assert_response :success
    assert_select 'title', "Contact | #{@base_title}"
  end

Add contact to the routing, add the contact action to the controller, create a view page and enter Listing 3.40 to pass the test.

routes.rb


Rails.application.routes.draw do
  get 'static_pages/home'
  get 'static_pages/help'
  get 'static_pages/about'
  get 'static_pages/contact'

  root 'application#hello'
end

static_pages_controller


def contact
end

ruby:contact.html.erb


<% provide(:title, "Contact") %>
<h1>Contact</h1>
<p>
  Contact the Ruby on Rails Tutorial about the sample app at the
  <a href="https://railstutorial.jp/contact">contact page</a>.
</p>

[3.4.4. Routing setting exercise]

  1. By adding root routing to Listing 3.41, you can now use a Rails helper called root_url (same as before when static_pages_home_url was available). Try writing a test for root routing, replacing the part marked FILL_IN in Listing 3.42. → Below

static_pages_controller_test.rb


  test "should get root" do
    get root_url
    assert_response :success
  end

2. Actually, since I wrote the code in Listing 3.41, the test for the previous task should already be green. In such cases, it is difficult to determine whether the test was successful before the change or whether it became successful after the change. To see if the code in Listing 3.41 is affecting the test results, comment out the root routing as shown in Listing 3.43 and see if it turns red (note that Ruby's commenting feature). Will be explained in 4.2.1). Finally, let's undo the commented out part (that is, back to Listing 3.41) and make sure the test turns green. → Certainly, the test results will change before and after commenting out.

Chapter 3 Summary

-Create a series of (almost) static pages that are the basis of web applications. Since it is not RESTful, each routing is set individually. ・ Basically, write the test first. It is important to draw the expected movement firmly in your head. ・ If you maintain the test, you can rest assured when refactoring. -The common parts of the view can be summarized in /layouts/application.html.erb. -Variables defined in the controller can be used in the ERB file.

Next, let's go to Chapter 4

Go to Chapter 4! Click here for Chapter 2 Click here for premise and author status for learning

A glossary that somehow captures the image

-Compile Translate from human to machine language. It seems that the program we wrote needs to be converted so that it can be understood because it cannot be understood directly on the computer side.

・ Refactoring Organize the internal structure of the source code without changing the behavior seen from the outside of the program. It's important to test each refactoring.

・ SEO (Search Engine Optimization) SEO measures = Display at the top of search results. Important in web marketing.

・ Assert_response Claim that the response has a specific status code. If you specify: success, you have specified status code 200-299. (Status Code List)

・ Assert_select Checks if a given element meets the criteria. It might be a convenient assertion. There are two formats. For more information, go to the Rails Guide. ](Https://railsguides.jp/testing.html)

Recommended Posts

(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 11]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 1]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 14]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 12]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 5]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 3]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 4]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 8]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 6]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 13]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 9]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 10]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 7]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 2]
(Giri) A local government employee in his twenties works on a Rails tutorial [Introduction]
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Rails Tutorial Chapter 5] Create a layout
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
(Ruby on Rails6) Creating data in a table
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
How to display a graph in Ruby on Rails (LazyHighChart)
Apply CSS to a specific View in Ruby on Rails