Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]

3.1 Setup

[Practice]

    1. Check to see if Bitbucket correctly renders the Markdown notation README (Listing 3.3) as HTML.
  1. Go to the production (Heroku) root URL and see if the deployment was successful.

⇒Please try it as it is.

3.2 Static page

■ Controller A controller is a container that bundles a collection of (basically dynamic) web pages. → I don't understand a little.

■rails generate controller StaticPages home help Static_pages_controller.rb is generated in the controller. in routes.rb ・ Static_pages / home ・ Static_pages / help Is generated.

At this point, enter "static_pages / home" in the address bar to move. The destination to move to is "home.html.erb" in the view.

■ HTTP method There are four types: GET, POST, PATCH, and DELETE. GET: Call page POST: Used when the user inputs something and sends it. PATCH: Probably used for updates. DELETE: Used for deletion

[Practice]

    1. Try creating a controller called Foo and adding bar and baz actions in it. → Use rails generate to generate the controller. rails generate controller Foo bar baz
  1. Try removing the Foo controller and related actions using the techniques introduced in Column 3.1. See column →rails destroy controller Foo bar baz

3.3 Start with a test

■ Test case, test suite One test is called a test case. A collection of test cases is called a test suite.

■ Test-driven design A method to proceed with development after writing test code

■static_pages_controller_test.rb It is generated at the same time as rails g.

test/controllers/static_pages_controller_test.rb


  test "should get home" do
    get static_pages_home_url
    assert_response :success
  end

This file already inherits ActionDispatch :: IntegrationTest, so I understand that's what it is. Generate a new test called "should get home". Access with "get static_pages_home_url". Judge whether it is successful or not with "assert_response: success".

■touch app/views/static_pages/about.html.erb In the touch app / views / static_pages directory Generate a file about.html.erb.

You can also right-click and select "Create File".

■about.html.erb You don't need .

3.4 A slightly dynamic page

→ Since this page is being refactored, it's okay to postpone it in the worst case. ■mv app/views/layouts/application.html.erb layout_file Move application.html.erb to layout_file.

■assert_select "title", "Home | Ruby on Rails Tutorial Sample App" Check if the content of the title tag of the page you moved to is "" Home | Ruby on Rails Tutorial Sample App "".

[Practice]

    1. Did you notice that the StaticPages controller tests (Listing 3.24) had some iterations? Especially the basic title "Ruby on Rails Tutorial Sample App" has the same content for each test. 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). → It's annoying to see a lot of words "Ruby on Rails Tutorial Sample App", right? It is a story.

Substitute the string Ruby on Rails Tutorial Sample App for @base_title and We are developing each.

■ Provide method Pass the parameter with the provide method and receive it with the yield method. By unifying the titles, everything except the contents of the tag is the same.

■app/views/layouts/application.html.erb What forms the basis of design.

[Practice]

    1. Create a Contact page in your sample application 17 (Hint: First, refer to Listing 3.15 and there is a page with the URL / static_pages / contact titled "Contact | Ruby on Rails Tutorial Sample App" Let's first create a test to see if it is, and then 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.)
  1. Create a file called contact.html.erb in static_pages /.

routes.rb


<% 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>
  1. Make a test. Just change the name to contact

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
  1. Set routes

routes.rb


get  'static_pages/contact'
  1. Pass the path through the controller.

static_pages_controller.rb


def contact
end

[Practice]

    1. With the addition of root routing to Listing 3.41, you can now use a Rails helper called root_url (same as before static_pages_home_url). Try writing a test for root routing, replacing the part marked FILL_IN in Listing 3.42.

statc_pages is not needed as root does not belong to any directory

static_pages_controller_test.rb


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

3.5 Finally

3.6 Advanced setup

Recommended Posts

Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
Chewing Rails Tutorial [Chapter 2 Toy Application]
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 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)
[Rails Tutorial Chapter 4] Rails-flavored Ruby
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] Second Half
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] First Half
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
Rails Tutorial (4th Edition) Memo Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
rails tutorial
rails tutorial
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 3
rails tutorial
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 8
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
Rails tutorial test
Rails tutorial memorandum 1
Start Rails Tutorial
[Beginner] Rails Tutorial
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)