Rails Tutorial Chapter 3 Notes

It is a memorandum of Chapter 3.

environment

Rails 6.0.3 Ruby 2.6.3

table of contents

How to undo with 1 command operation 2 HTTP method 3 Empty methods behave differently in Rails 4 Decrypt default test 5 Routing Introduction of 6 minitest reporters 7 Introduced Guard test automation

1 How to restore by command operation

Occasional command operation failure. If you delete only the wrong file, the code inserted in another file will remain when that file is created, so the model or controller generated by the command must be deleted by the command as well.

#Controller creation
$ rails generate controller StaticPages home help

#Controller removed
$ rails destroy controller StaticPages home help

You can delete the model in the same way.

Next is migration

#Migration changes (updates)
$ rails db:migrate

#Revert migration to previous state
$ rails db:rollback

#Make the migration the initial state (VERSION)=I want to return to 0)
$ rails db:migrate VERSION=0

The VERSION number is set every time you do rails db: migrate, so you can replace it with another number.

2 HTTP method

HTTP (HyperText Transfer Protocol) has four basic operations: `GET` `` `POST PATCH DELETE```.

GET POST PATCH Delete
Get the page Create based on the value entered in the form update Delete

3 Empty method behavior

In Rails, even an empty method behaves differently

app/controllers/staticpages_controller.rb


class StaticPagesController < ApplicationController
  def home
  end
  .
  .
end

Even in this case, the StaticPagesController class inherits from the Rails class called ApplicationController, so when you access/static_pages/home, you'll see a view called home.html.erb.

4 Decrypt the default test

test/controllers/static_pages_controller_test.rb


require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get home" do
    get static_pages_home_url
    assert_response :success
    assert_select "title", "Home | Ruby on Rails Tutorial Sample App"
  end

  .
  .
end

Confirmation of this test is a method called "assertion", which gets an action and confirms that it works normally.

The above test flow is 1, GET request for home action (display page) 2, Response becomes successful (200) 3, The character string described in the ``` ` `` tag is displayed.</p> <h3>5 Routing</h3> <h4><strong><code>config/routes.rb</code></strong></h4> <pre><code class="language-rb"> Rails.application.routes.draw do root 'static_pages#home' get 'static_pages/home' . . end </code></pre> <p>If you set up root routing, you can use a Rails helper called root_url.</p> <h3>Introduction of 6 minitest reporters</h3> <p>When minitest reportes are installed, the test results are displayed in color.</p> <h4><strong><code>Gemfile</code></strong></h4> <pre><code class="language-python"> . group :test do . gem 'minitest', 'version' gem 'minitest-reportes', 'version' . end . </code></pre> <h4><strong><code>test/test_helper.rb</code></strong></h4> <pre><code class="language-rb"> . require 'rails/test_help' require "minitest/reportes" Minitest::Reportes.use! . . </code></pre> <h3>7 Introduced Guard test automation</h3> <p>By setting Guard, for example, if you change <code>`home.html.erb```,</code> `static_pages_controller_test.rb``` will be executed automatically.</p> <h4><strong><code>Gemfile</code></strong></h4> <pre><code class="language-python"> . group :test do . gem 'minitest-reportes', 'version' gem 'guard', 'version' gem 'guard-minitest', 'version' end . . </code></pre> <h5>Initialize</h5> <pre><code>$ bundle exec guard init </code></pre> <p>We will customize the generated <code> Guardfile</code>, but omit it here. Since the setting changes depending on when using Rspec etc., I will proceed according to the text in Rail Tutorial, but I would like to set it myself later.</p> <pre><code>$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf $ sudo sysctl -p </code></pre> <p>In the case of cloud IDE, execute the command on the above two lines and set so that all files in the project can be monitored by Guard.</p> <pre><code>$ bundle exec guard </code></pre> <p>Finally, execute the above command in the new terminal to start it.</p> <pre><code>$ bin/spring stop $ bundle exec guard </code></pre> <p>If the test fails unnecessarily, reboot with the above command. Spring: A tool that pre-loads Rails information and speeds up testing</p> <!-- ENDDDDDDDDDDDDDDDDDDDDDDDDDDDDD --> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- post_new_ui_horiz --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5469278205356604" data-ad-slot="4209814965" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div style="margin-top: 30px;"> <div class="link-top" style="margin-top: 1px;"></div> <p> <font size="4">Recommended Posts</font> <!-- BEGIN LINK ************************* --> <div style="margin-top: 10px;"> <a href="/en/2ae1406da9746297b8e5">Rails Tutorial Chapter 5 Notes</a> </div> <div style="margin-top: 10px;"> <a href="/en/450cf8760b4fefb93bcd">Rails Tutorial Chapter 3 Notes</a> </div> <div style="margin-top: 10px;"> <a href="/en/bb97737b98e418125ec2">Rails Tutorial Chapter 4 Notes</a> </div> <div style="margin-top: 10px;"> <a href="/en/e3c4f19993ae17403b11">Rails Tutorial Chapter 8 Notes</a> </div> <div style="margin-top: 10px;"> <a href="/en/281cee1d2b680bdc2a89">rails tutorial Chapter 6</a> </div> <div style="margin-top: 10px;"> <a href="/en/5c48c81e8b5563990064">rails tutorial Chapter 1</a> </div> <div style="margin-top: 10px;"> <a href="/en/81bd8f6b104def72a38a">rails tutorial Chapter 7</a> </div> <div style="margin-top: 10px;"> <a href="/en/8bf5387f78fa6b3ff510">rails tutorial Chapter 5</a> </div> <div style="margin-top: 10px;"> <a href="/en/983c0fbf3fcd7e11a35f">rails tutorial Chapter 10</a> </div> <div style="margin-top: 10px;"> <a href="/en/aa2607986b624f36ccae">rails tutorial Chapter 9</a> </div> <div style="margin-top: 10px;"> <a href="/en/c8c3de9a4d0cfa064a3d">rails tutorial Chapter 8</a> </div> <div style="margin-top: 10px;"> <a href="/en/6284079eddb2fe3b3de5">Rails Tutorial Chapter 3 Learning</a> </div> <div style="margin-top: 10px;"> <a href="/en/903becb9319e3678f8ae">Rails Tutorial Memorandum (Chapter 3, 3.1)</a> </div> <div style="margin-top: 10px;"> <a href="/en/c8f423bb87500d5a192e">Rails Tutorial Chapter 4 Learning</a> </div> <div style="margin-top: 10px;"> <a href="/en/cc8fa392c04026b1da33">Rails Tutorial Chapter 1 Learning</a> </div> <div style="margin-top: 10px;"> <a href="/en/ccab416759ae6d748f05">Rails Tutorial Chapter 2 Learning</a> </div> <div style="margin-top: 10px;"> <a href="/en/f5c5bc15c02ff233d896">rails tutorial fighting notes Ⅲ</a> </div> <div style="margin-top: 10px;"> <a href="/en/f6a353c2b99255069171">Rails Tutorial Memorandum (Chapter 3, 3.3.2)</a> </div> <div style="margin-top: 10px;"> <a href="/en/31c46a270c3f35c60613">rails tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/6e0bd627f84501f2088d">rails tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/79d90fdf98d8797c806f">rails tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/7d314c5c1a551c77c777">rails tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/abd20fe33e91bca94800">[Rails Tutorial Chapter 4] Rails-flavored Ruby</a> </div> <div style="margin-top: 10px;"> <a href="/en/cadfc2d347fd11849d19">rails tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/d2257984edd4174e28b5">rails tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/e555c1723c04699d1f8d">rails tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/0c0d4253348641cf529d">[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2</a> </div> <div style="margin-top: 10px;"> <a href="/en/4aba1f02bf215ef4e0e4">[Rails Tutorial Chapter 5] Create a layout</a> </div> <div style="margin-top: 10px;"> <a href="/en/5c618551eec9ca0b992a">rails tutorial chapter 10 summary (for self-learning)</a> </div> <div style="margin-top: 10px;"> <a href="/en/9811a1e9254007cfae7d">Chewing Rails Tutorial [Chapter 2 Toy Application]</a> </div> <div style="margin-top: 10px;"> <a href="/en/edf39cef737b668643e2">Rails Tutorial (4th Edition) Memo Chapter 6</a> </div> <div style="margin-top: 10px;"> <a href="/en/3114420b4ead7076e2c7">Rails tutorial test</a> </div> <div style="margin-top: 10px;"> <a href="/en/65dec681fd0e54dcdad7">Rails tutorial memorandum 1</a> </div> <div style="margin-top: 10px;"> <a href="/en/74d0df1b15943879f369">Rails tutorial memorandum 2</a> </div> <div style="margin-top: 10px;"> <a href="/en/de1bbd1443b138385ba3">Start Rails Tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/e724f771d1415f526cc3">[Beginner] Rails Tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/f0a168c44b1fae6509cc">html & rails notes</a> </div> <div style="margin-top: 10px;"> <a href="/en/04d5fd3bb8498d7957dd">Rails Tutorial 6th Edition Learning Summary Chapter 10</a> </div> <div style="margin-top: 10px;"> <a href="/en/087efb655fec1a678673">Rails Tutorial 6th Edition Learning Summary Chapter 7</a> </div> <div style="margin-top: 10px;"> <a href="/en/265f29b77f3126d3a9df">Rails Tutorial 6th Edition Learning Summary Chapter 4</a> </div> <div style="margin-top: 10px;"> <a href="/en/48bb1a43ffac4290959f">Rails Tutorial 6th Edition Learning Summary Chapter 9</a> </div> <div style="margin-top: 10px;"> <a href="/en/497fc90d7c6f9bc9a0ff">Rails Tutorial 6th Edition Learning Summary Chapter 6</a> </div> <div style="margin-top: 10px;"> <a href="/en/6949dc3131d4f34e27c0">Rails Tutorial 6th Edition Learning Summary Chapter 5</a> </div> <div style="margin-top: 10px;"> <a href="/en/86da084f32e502ff4393">Rails Tutorial 6th Edition Learning Summary Chapter 2</a> </div> <div style="margin-top: 10px;"> <a href="/en/8e7b72bd00be7b44dab5">Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5</a> </div> <div style="margin-top: 10px;"> <a href="/en/ccebe817b40b1152feba">Rails Tutorial 6th Edition Learning Summary Chapter 3</a> </div> <div style="margin-top: 10px;"> <a href="/en/fce014c8aeee9a0d7201">Rails Tutorial 6th Edition Learning Summary Chapter 8</a> </div> <div style="margin-top: 10px;"> <a href="/en/1eedbe5eebc64fd37d70">[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6</a> </div> <div style="margin-top: 10px;"> <a href="/en/285edb2974d3de5b8792">Chapter 4 Rails Flavored Ruby</a> </div> <div style="margin-top: 10px;"> <a href="/en/4115c8e57894c81a35f7">[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3</a> </div> <div style="margin-top: 10px;"> <a href="/en/45d84489fb300afc06d0">Rails Tutorial cheat sheet</a> </div> <div style="margin-top: 10px;"> <a href="/en/6670d39ee5d86c4e819e">Rails Tutorial Chapter 1 From Zero to Deployment [Try]</a> </div> <div style="margin-top: 10px;"> <a href="/en/7df42ec26941ce7ca0ac">[Rails] Learning with Rails tutorial</a> </div> <div style="margin-top: 10px;"> <a href="/en/c7d6af0d0378ac568874">Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]</a> </div> <div style="margin-top: 10px;"> <a href="/en/c82ca0e0fc7a97376586">[Rails tutorial] A memorandum of "Chapter 11 Account Activation"</a> </div> <div style="margin-top: 10px;"> <a href="/en/08945c24869294e93f59">Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)</a> </div> <div style="margin-top: 10px;"> <a href="/en/269b2fae825262db03e7">[Ruby on Rails Tutorial] Error in the test in Chapter 3</a> </div> <div style="margin-top: 10px;"> <a href="/en/bca5ca7a8bdc95886448">Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment</a> </div> <div style="margin-top: 10px;"> <a href="/en/bcb17c747bbe075035c7">Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)</a> </div> <div style="margin-top: 10px;"> <a href="/en/ccf1f7f57b6627034226">Ruby on Rails Tutorial Troublesome notes when running on Windows</a> </div> <div style="margin-top: 10px;"> <a href="/en/4f5898251fbca04de5b5">11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11</a> </div> <!-- END LINK ************************* --> </p> </div> </div> </div> <div class="footer text-center" style="margin-top: 40px;"> <!-- <p> Licensed under cc by-sa 3.0 with attribution required. </p> --> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.2/build/highlight.min.js"></script> <script> $(document).ready(function() { var cfg_post_height = 60; var cfg_per = 0.51; var ads_obj = $('<ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-5469278205356604" data-ad-slot="7950405964"></ins>'); $('pre code').each(function(i, e) {hljs.highlightBlock(e)}); function getDocumentOffsetPosition( el ) { var _x = 0; var _y = 0; while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) { _x += el.offsetLeft - el.scrollLeft; _y += el.offsetTop - el.scrollTop; el = el.offsetParent; } return { top: _y, left: _x }; } if ( $( "#article202011" ).length ) { var h1_pos = getDocumentOffsetPosition($('h1')[0]); var footer_pos = getDocumentOffsetPosition($('.link-top')[0]); var post_distance = footer_pos.top - h1_pos.top; // console.log('h1_pos: '+ h1_pos.top); // console.log(cfg_post_height) if((post_distance/h1_pos.top)>=cfg_post_height) { // console.log('tesssssssssssssssssssssssssssssssss'); $( ".container p" ).each(function( index ) { var p_tag_pos = $(this).position().top; var dis = p_tag_pos - h1_pos.top; var per = dis/post_distance; if(per>cfg_per) { ads_obj.insertAfter($(this)); (adsbygoogle = window.adsbygoogle || []).push({}); console.log( index + ": " + $( this ).text() ); return false; } }); } } }); </script> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <!-- ads --> <script data-ad-client="ca-pub-5469278205356604" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" type="d7540fe192d881abe59fcf57-text/javascript"></script> <!-- end ads --> </body> </html><script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="88feb9ae3b92aeda4de93154-|49" defer></script>