[RUBY] Summary of basic knowledge of Rails acquired by progate

What kind of person is the article for?

--People who have just started learning Rails --People trying to make their first app with Rails --People who are used to Rails but want to review a little

What kind of article

This is an article that the author, who is a beginner of Rails, lists what he learned in the Rails course of progate that he completed the other day. Since it is supplemented with information from other articles in some places, there are some differences from the content expressed and explained by progate management.

Various concepts


A computer that provides data at the request of a client (browser). In order to display the Rails-made app on the browser, you need to start this server.


An HTML file to create the look of the page. In the interaction between the browser and Rails, Rails returns a view to display the page. The extension is .html.erb. erb is an abbreviation for Embedded Ruby, a mechanism for embedding Ruby scripts in HTML files. One of the three elements needed to create a page, similar to the controller routing below.


For returning a view to the browser. Each controller is created according to the purpose. For example, post-related functions and user-related functions are specified in separate controller files.


A feature in the controller to find the view to return to the browser. Expressed as a method in the controller file.


It's like a correspondence table that decides which controller and which action to process the sent URL.


A place to store your data.


A table that manages data in the database. The data for each row is called a record, and the data for each column is called a column.


A class that interacts with the database. Data can be stored in a table by creating an instance of the model and saving it.

Migration file

A file that tells the database to make changes.


Code used in view files

<% %>

ex)<%= @post %>

When embedding Ruby code in an HTML file, write it before and after the code. If you want the code to be displayed in the browser, write <% =%> before and after the code.

link_to method

<%= link_to("Character to display", "URL") %>
ex) <%= link_to("Post list", "/posts/index")%>

Create a link. Used in HTML files. By adding {method: "post"} to the third argument, it will match the routing defined as post.


<%= form_tag("URL") do %>
<% end %>

<%= form_tag("/posts/update") do %>
 <textarea name="content"><%= @post.content %><textarea>
  <input type="submit" value="Post">
<% end %>

The data entered in the form can be sent to the specified URL. However, form_tag alone is meaningless, and by specifying the name attribute in the textarea tag (or input tag), the hash with the name attribute as the key can be transmitted to the action side. As an aside, by putting a value between the textarea tags as described above, you can enter from the continuation of the content that was originally entered when re-entering the form.


<%Instance name.errors.full_messages.each do |message| %>
  <%= message %>
<% end %>

<% @posts.errors.full_messages.each do |message| %>
  <%= message %>
<% end %>

Output an error message. If validation fails when you call the save method, Rails will automatically generate an error message, so you can display everything by using the each statement.



<%= yield %>

Each view file is assigned to the yield described in application.html.erb. application.html.erb is a view file that describes the layout that applies to the entire site.

Code used in controller file


@post = "hogehoge"

If you define it in the controller file, you can use it when embedding Ruby code in the view file.

new method

Model name.new(Column name:value)
ex) post = Post.new(content: "hogehoge")

Create an instance from the model.

save method

Instance name.save
ex) post.save

Save the created instance in the table.

all method

Model name.all
ex) posts = Post.all

Get all the records in the table.

find_by method

Model name.find_by(Column name:value)
ex) post = Post.find_by(id: 1)

Get one data that matches a certain condition.

where method

Model name.where(Column name:value)
ex) posts = Post.where(id: 1)

Acquire multiple data that meet a certain condition.

redirect_to method

ex) redirect_to("/posts/index")

You can transfer to the specified page.

render method

render("Folder name/file name")
ex) render("posts/edit")

You can view the view directly without going through another action. It is often used when data saving fails.

order method

Model name.order(Column name: :Sorting order)
ex) @posts = Post.all.order(created_at: :desc) 

Sort the acquired data. : desc represents descending order and: asc represents ascending order.

destroy method

Instance name.destroy
ex) post.destroy

Deletes the specified data (instance) from the database.

Variable session

session[:Key name] =value
ex) session[:user_id] = @user.id

This is to keep the logged-in user's information even when the page is moved. You can prevent you from being logged in by substituting nil.


ex1) @id = params[:id]
ex2) @post = Post.new(content: params[:content])

Example 1. Get the value of: ○○ of the URL set in the routing. Example 2. Receive the input contents of the form with name = "○○".


before_action Common processing for all actions
ex) before_action :set_current_user, {only: [:edit, :update]}

The described process is always executed before calling any action. Write at the top of the file. By using {only: [: action name]}, it is possible to limit the action to be executed.

Code used in the routing file

get method

app name/config/routes.rb

get "URL" => "Controller name#Action name"
ex) get "/posts/index" => "posts#index"

Get the specified information. If you don't want to change the database, use get. get and the post below are called HTTP methods.

post method

app name/config/routes.rb

post "URL" => "Controller name#Action name"
ex) post "/posts/create" => "posts#create"

The method used to change the value of session when changing the database. It seems that there are many other HTTP methods, but I haven't learned them yet, so I'll omit them here.

Named parameters

app name/config/routes.rb

get "posts/:id" => "posts#show"

If you put a string starting with: in the URL part of the routing, that string will be recognized as a parameter. Therefore, in this example, all URLs such as / posts / ○○ can be directed to the show action. For that reason, routings like / posts / index must be written before it so that they don't get caught in the routing for / posts /: id.

Code used in the model file


validates :Column name to verify, {Contents to be verified}
ex) validates :content, {presence: true}

Check the data (validation) so that malicious data is not stored in the database. The main verification contents are as follows.

Verification content meaning
presecse: true Check if the value in that column exists
length: {maximum:word count} Prevent data from saving more than the specified number of characters
uniqueness: true Check if duplicate data exists in the database

Code used in the migration file

add_column :table name, :Column name, :Data type
ex) add_column :users, :image_name, :string

Add a column to an existing table. Write in the change method.

Command line

--rails new app name A folder with the same name as the entered application name will be created, and the folders and files required for development will be prepared in it.

--rails g controller controller name action name Creating a controller. g can be generate. The controller name and action name are often the same as the HTTP method (URL).

--rails g model model name column name: data type Creating migration and model files. The model name is singular and the first letter is capitalized.

--rails g migration filename Create only the migration file. The file name should be something descriptive, such as add_image_name_to_users.


progate is good.

Referenced articles

-[Introduction] What is a server? Easy-to-understand explanation of roles and types! -View Basics -Thorough explanation of Rails model! Introducing three things you need to know -Node.js + Express URL routing

Referenced books

-[Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field](https://www.amazon.co.jp/%E7%8F%BE%E5%A0%B4%E3%81%A7%E4%BD% BF% E3% 81% 88% E3% 82% 8B-Ruby-Rails-5% E9% 80% 9F% E7% BF% 92% E5% AE% 9F% E8% B7% B5% E3% 82% AC% E3% 82% A4% E3% 83% 89-% E5% A4% A7% E5% A0% B4-ebook / dp / B07JHQ9B5T)

Recommended Posts

Summary of basic knowledge of Rails acquired by progate
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Basic knowledge of Ruby on Rails
[Java] Basic summary of Java not covered by Progate ~ Part 2 · List ~
Summary of basic migration knowledge rails db: rollback and column addition / deletion
Basic knowledge of SQL statements
Summary of basic functions of ImageJ
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Summary of rails validation (for myself)
[Rails] Introduction of Rubocop by beginners
[Rails] Summary of complicated routing configurations
[Basic knowledge of Java] Scope of variables
[Rails] Temporary retention of data by session
Basic knowledge
Summary of Docker understanding by beginners ② ~ docker-compose ~
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
[Java] Personal summary of conditional statements (basic)
Basic knowledge of Java development Note writing
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
[Basic knowledge of Java] About type conversion
Rails: A brief summary of find, find_by, where
Way of thinking when studying basic program knowledge
A summary of only Rails tutorial setup related
[Ruby] Basic knowledge of class instance variables, etc.
[Java] Personal summary of classes and methods (basic)
Summary of Docker understanding by beginners ① ~ docker run -p ~
[Note] Summary of rails login function using devise ①
Docker basic summary
Java knowledge summary
Java basic knowledge 1
Rails 6.0 Routing Summary
Rails basic philosophy
rails db: 〇〇 Summary
Summary of frequently used commands in Rails and Docker
A review of the code used by rails beginners
Summary of revisions (new era) support by Java version
A brief summary of Rails association options (foreign_key, primary_key)
Image processing: The basic structure of the image read by the program
Summary of knowledge required to pass Java SE8 Silver
Introduction to Java for beginners Basic knowledge of Java language ①