[RUBY] A review of the code used by rails beginners


I learned rails with progate and dot installation and explained the code used. It will be used as an output to improve the efficiency of learning, so I would be grateful if you could point out any mistakes.

Code and description used

Command line

$ rails new myblog

Create an application.

$ rails g model Post title:string body:text

Create a model and table. A model is a class that interacts with the database. string creates a string and text creates a long string.

$ rails db:migrate

Reflect changes in the database

$ rails g controller Posts

Create a controller. controller: It has the role of receiving requests from the browser and exchanging with model / view.

$ rails routes

Check the contents of the set routing. Routing: Recognizes the received URL and assigns it to the appropriate in-controller action.

$ rails c

Run rails console

$ rails s

Start the rails server.

Code used in the controllser.rb file

@post = Post.find_by(id: params[:id])

Find the appropriate id in the database and assign that data to @ post.


Save the contents of @ post to the database.


Delete the contents of @ post from the database. When writing a link to a destroy action, write a method.

def update
    redirect_to("Folder name/file name")

redirect_to makes a page transition to the specified URL after executing the action.

def update
    render("Folder name/file name")

render displays the view directly at the specified URL without going through another action.

def update
    flash[:notice] = "Enter the characters you want to display"

Display a flash message in the place where <% = flash [: notice]%> is described. The feature of flash messages is that they are displayed only once on the page.

def index
    @posts = Post.all

Get all the data.

Steps to create a new page

Let's take a look at the steps to create a detail page as an example. Therefore, the action name is show. The controller name is posts described in the command line item. There are three steps, but it doesn't matter which one you start with.


def show

① Add the show action to the controller file.


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

② Add routing to the show action.

③ Create show.html.erb in the ʻapp viewsposts` folder in the created application.

Create a link

You will see a link that takes you to the details page from ʻindex`.


<%= link_to(post.content, "/posts/#{post.id}") %>

The link_to method corresponds to the<a>tag. In this case, the text described in post becomes a link and moves to the page of ʻid` obtained from the database.

Save the content entered in the form to the database


<%= form_tag("/posts/create") do %>
  <textarea name="content"></textarea>
<% end %>

form_tag sends the value of the input form. Set the name attribute to specify the value to send.


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

Save the data received from the input form in the database. After saving, go to the ʻindex.html.erb` page.

Add a column


rails g migration filename

① Create a migration file.

  add_column :table name, :Column name, :Data name

(2) Describe the contents of the migration file.


rails bg:migrate

③ Save in the database.


Anyway, I would like to continue learning to the extent that I can make deliverables with rails!

Recommended Posts

A review of the code used by rails beginners
[Rails] Introduction of Rubocop by beginners
The code I used to connect Rails 3 to PostgreSQL 10
A review note of the Spring Framework Resource interface
[Enum] Let's improve the readability of data by using rails enum
[Rails] Register by attribute of the same model using Devise
A note about the seed function of Ruby on Rails
[Rails] How to display the list of posts by category
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
What beginners can learn by creating a simple Rails API
The story of the first Rails app refactored with a self-made helper
[Rails] Check the contents of the object
Explanation of the order of rails routes
[Rails] Suppress unnecessary SQL by utilizing the cache control of the association
[RSpec on Rails] How to write test code for beginners by beginners
Check the migration status of rails
A memorandum of the FizzBuzz problem
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)
Replace preview by uploading by clicking the image in file_field of Rails
[Rails] Get a unique record from the table joined by the join method of Active Record (Rails Tutorial Chapter 14)
A collection of methods often used when manipulating time with TimeWithZone of Rails
[Rails] Articles for beginners to organize and understand the flow of form_with
How to make the schema of the URL generated by Rails URL helper https
Get the value of enum saved in DB by Rails with attribute_before_type_cast
A review note for the class java.util.Scanner
[Rails] Temporary retention of data by session
The identity of params [: id] in rails
Try using the Rails API (zip code)
Summary of Docker understanding by beginners ② ~ docker-compose ~
part of the syntax of ruby ​​on rails
The process of understanding Gemfile by non-engineers
[Swift] Termination of the program by assertion
What Rails beginners learned by solving errors
A review note for the class java.util.Optional
[Rails] Change the label name of f.label
Rails [For beginners] Implementation of comment function
Rails Basics of creating a new application
A review note for the class java.util.Objects
Explanation of Ruby on rails for beginners ①
The process of introducing Vuetify to Rails
The contents of the data saved by CarrierWave.
Find the difference from a multiple of 10
A review note for the package java.time.temporal
[Implementation] Eliminate the ominous smell of code
Rails Review 1
[Rails 6] Change redirect destination at the time of new registration / login by devise
How to display the amount of disk used by Docker container for each container
I used it without knowing the O / R mapping of rails, so I checked it.
Call a method of the parent class by explicitly specifying the name in Ruby
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
How to make a unique combination of data in the rails intermediate table
[Rails 6 + Action Mailbox] Those who extracted the plain text of Gmail received by Action Mailbox
Rails Review 2
[Rails] How to decide the destination by "rails routes"
[Order method] Set the order of data in Rails
Rails: A brief summary of find, find_by, where
A summary of only Rails tutorial setup related
[Rails] Button to return to the top of the page
A memorandum to clean up the code Ruby
A brief summary of Bootstrap features for beginners
[Ruby on Rails] Until the introduction of RSpec