It is a memorandum of Chapter 3.
Rails 6.0.3 Ruby 2.6.3
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
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.
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 |
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.
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 ```
config/routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/home'
.
.
end
If you set up root routing, you can use a Rails helper called root_url.
When minitest reportes are installed, the test results are displayed in color.
Gemfile
.
group :test do
.
gem 'minitest', 'version'
gem 'minitest-reportes', 'version'
.
end
.
test/test_helper.rb
.
require 'rails/test_help'
require "minitest/reportes"
Minitest::Reportes.use!
.
.
By setting Guard, for example, if you change `home.html.erb```,
`static_pages_controller_test.rb``` will be executed automatically.
Gemfile
.
group :test do
.
gem 'minitest-reportes', 'version'
gem 'guard', 'version'
gem 'guard-minitest', 'version'
end
.
.
$ bundle exec guard init
We will customize the generated Guardfile
, 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.
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -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.
$ bundle exec guard
Finally, execute the above command in the new terminal to start it.
$ bin/spring stop
$ bundle exec guard
If the test fails unnecessarily, reboot with the above command. Spring: A tool that pre-loads Rails information and speeds up testing
Recommended Posts