[RUBY] [Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapters 4 and 5


Since there was a lot of basic Ruby grammar in Chapter 4 of the Rails tutorial, I would like to output what I learned in Qiita together with Chapter 5.

What you learned in Chapter 4

In Chapter 4, there was a lot of talk about Ruby, so the impression that the code written in Chapter 4 was mainly about the contents of custom helpers. In Rails View, you can create new methods. This is what is called a custom helper. Other than the custom helper, it was about Ruby, so I will omit it here. I will summarize in Qiita how the basic grammar of Ruby will be useful for Rails development.

What you learned in Chapter 5

In Chapter 5, you will first learn how to use partial templates. You can insert it into your layout by using a Rails helper call called render. You can reduce the amount of CSS description by using Sass, but since I was doing Web production myself, the review is a pass.

Using what you created static_pages in Chapter 3, it's better to use/about instead of/static_pages/about. So, in Chapter 5, I learned how to write/about.

Page name URL Named route
Home / root_path
About /about about_path
Help /help help_path
Contact /contact contact_path
Sign up /signup signup_path
Log in /login login_path

It is necessary to modify routes.rb to realize the route URL as shown in the above table. Basically, write in _path format, and write in _url only in the case of redirect. Writing with a get rule allows you to call the about action of the StaticPages controller when a GET request is sent to/about. You can remove'static_pages/home' by using root_path or root_url.


Rails.application.routes.draw do
  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
  get  '/signup', to: 'users#new'

Along with this, we will also modify the Static Pages controller. By defining the route, you can use the route (root_url, help_path, etc.) in the layout.

After modifying the route, test the link (integration test). The purpose of the test is to send a GET request to the root URL. Check if the correct page template is drawn. Check if the links to each page work properly.

After the integration test, implement user registration. The first thing to do when registering as a user is to generate a Users controller. Let new be the action for new user registration. To generate a Users controller, run the following command.

$ rails g controller Users new

If you can generate the controller file, this is the end of Chapter 5.

At the end

To be honest, I felt that I was studying as usual after the six consecutive holidays during the year-end and New Year holidays. I had a habit, and I didn't have to rush. I will study Chapter 6 from tomorrow, but I thought that I would just do it as usual.

Recommended Posts

[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapters 4 and 5
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
Rails Tutorial Records and Memorandum # 0
What you learned about symbols
What I learned in Java (Part 4) Conditional branching and repetition
What you learned when you acquired Java SE 8 Silver and Gold
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What I learned from studying Rails
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
What I have learned in Java (Part 1) Java development flow and overview
Why you need attr_accessor and why you don't (Thanks to Rails): Rails Tutorial Memorandum--Chapter 9
What to do if Cloud9 is full in the Rails tutorial
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
Things to remember and concepts in the Ruby on Rails tutorial
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
What is follow_redirect! Following ?: Rails Tutorial Memorandum-Chapter 7
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
Rails refactoring story learned in the field
What Rails beginners learned by solving errors
How to use MySQL in Rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
Where I got stuck in today's "rails tutorial" (2020/10/08)
(Basic authentication) environment variables in rails and Docker
rails tutorial About account activation in production environment
Where I got stuck in today's "rails tutorial" (2020/10/05)
Where I got stuck in today's "rails tutorial" (2020/10/06)
What I learned in Java (Part 2) What are variables?
Rails Reverse Lookup Dictionary (Gives what you want)
[Ruby] What if you put each statement in each statement? ??
Summary of what I learned in Spring Batch
Where I got stuck in today's "rails tutorial" (2020/10/07)
[Rails] Ranking and pagination in order of likes