[Ruby] Chewing the Rails Tutorial [Chapter 3 Creating almost static pages]

3 minute read

3.1 Setup


  1. Check if Bitbucket renders the Markdown README (List 3.3) correctly as HTML.
  2. Go to the root URL of your production environment (Heroku) and see if the deployment was successful.

⇒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 is the view “home.html.erb”. *The same applies to help.

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


  1. Create a controller called Foo and add the bar and baz actions to it. →Use rails generate to generate a controller. rails generate controller Foo bar baz

  2. Try removing the Foo controller and its associated actions using the techniques introduced in Box 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 write test code and then proceed with development

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


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

This file already inherits ActionDispatch::IntegrationTest, so Understand that it is. Generate a new test “should get home”. Access with “get static_pages_home_url”. Determine whether it is a success with “assert_response :success”.

■ touch app/views/static_pages/about.html.erb in touch app/views/static_pages directory Generate about.html.erb file.

You can also go to “Create File” from right click.

■ about.html.erb I don’t need <html><body>.

3.4 A slightly dynamic page

→ It’s a refactoring page, so it’s okay to put it off 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 whether the content of the title tag of the moved page is ““Home Ruby on Rails Tutorial Sample App””.


  1. Did you notice that the StaticPages controller test (Listing 3.24) had some iterations? In particular, the basic title “Ruby on Rails Tutorial Sample App” wrote the same content each time for each test. So, I’d like to solve this problem by using a special method called setup (a method that runs just before each test is run). First, let’s make sure the test in Listing 3.30 is green (in Listing 3.30, we use the technique of instance variable and string expression expansion that we touched on a bit in 2.2.2. 4.4.5 and 4.2, respectively. It will be explained in detail in section .2, so if you don’t understand it now, there is no problem.) →Isn’t it troublesome to see a lot of letters “Ruby on Rails Tutorial Sample App”? Is a story.

Substitute @base_title with the string Ruby on Rails Tutorial Sample App, Each is developing.

■Provide method Pass the parameter with the provide method and receive it with the yield method. By unifying the titles, the contents other than the contents of the <body> tag are made the same.

■ app/views/layouts/application.html.erb The core of the design.


  1. Create a Contact page in the sample application 17 (Hint: First, refer to Listing 3.15 and the page with the URL /static_pages/contact has the title “Contact Ruby on Rails Tutorial Sample App”) Let’s first create a test to check if it’s true, then 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.)
  2. Create a file called contact.html.erb in static_pages/.


<% provide(:title, "Contact") %>
  Contact the Ruby on Rails Tutorial about the sample app at the
  <a href="https://railstutorial.jp/contact">contact page</a>.
  1. Make a test. Just change the name to contact


test "should get contact" do
    get static_pages_contact_url
    assert_response :success
    assert_select "title", "contact | #{@base_title}"
  1. Set routes


  1. Thread the pass through the controller.


def contact


  1. With the addition of root routing in Listing 3.41, we now have a Rails helper called root_url available (as it did before with static_pages_home_url). Replace the part marked FILL_IN in Listing 3.42 and write a test for root routing.

root does not belong to any directory, so statc_pages is unnecessary


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

3.5 Finally

3.6 Advanced setup