I tried Rails beginner [Chapter 1]

About this article

This is a learning record for me, who was at a loss because the Rails tutorial that I started after completing the Web development course of Progate was too difficult, to be able to develop apps through Rails beginners.

What is Rails Beginner?

Rails Beginner This is a teaching material for programming beginners created by Yuya Maki, a freelance programmer. There are Chapters 0 to 3 (as of June 13, 2020), and we will create a question site according to the detailed explanation. The politeness and content that does not seem to be free. Thanks. .. ..

Chapter 1 "Let's make a question posting function"

(Chapter 0 is a simple VS Code setup, so I'll omit it.)

Go to desktop and create beginner_app (rails new command) Open the folder and start rails s

⚠️ rails s startup error

It is sometimes called Please run rails web packer. I will install what I was told until the startup is successful ➡️zsh: command not found: yarn I was told that there is no yarn. Install Homebrew and install yarn from brew (brew install yarn) Check the version when you're done (yarn --version)

If you access localhost: 3000 after starting up safely, that screen will appear

Make a page for the time being

-Creation of questions controller (rails g controller controller name) -Added index action to questions controller -Add to root file (get" / questions ", to:" questions # index ") -Create a view corresponding to the index action (manually create index.html.erb in the questions folder inside the view folder) -Add <% = @test%> after creating ← @test defined by the controller with the same name can be used in the view

Prepare a database to store questions

● Creating a model (interacting with the database) rails g model model name

Create a new question creation function

● Define new actions, route, create views ⭐️ in the view code<%= form_with(model: @question, local: true) do |f| %>What? Code that creates a form to save to the database. Meaning of local  do |f|Search for "block argument".

-Since the submit button of the created form is designed to access / params by post, route it as it is.

● Define a create action -Question.new (question_params) creates an instance with the value sent to the form, so assign it to @question. ➡️ Save to database with @ question.save ➡️ If the save is successful, go to the newly created page (redirect_to" / questions / new "), otherwise display the views / questions / new.html.erb file on the screen (render" questions / new " )

● Specify the elements to receive in the form -Define question_params, which is an argument of Question.new, under the private method. By using strong parameters (params: require (: model name) .permit (: table name)), only specified parameters can be accepted.

● Verify proper data – Validation -Specify after validates: table name in the question model file The contents to be specified include presence: true, which requires input, and the length of the character stringlength: {maximum or minimum: numerical value}. · Add flash to create action and application view ⭐️ [: notice] key ・ About [: alert] key: It is prepared in advance for notification / warning, but you can give the key any name you like.

Create a question display function

● Define and route show actions, create view files ● Routing when accessing localhost: 3000: root to:" URL "

This is the end of Chapter 1.


What i learned

I realized that I didn't have the basics in my head, so I decided to make it more solid (small average feeling).

● Routing ➡️ Controller ➡️ Processing is performed in the flow of the view ● By assigning an empty instance to a variable with the new action and passing that variable as an argument in the view, it will be judged as a new creation (instance is empty = new creation) ● You can easily create a form with the form_with helper. Text_field is a one-line form, text_area is a multi-line form. ● Strong parameters: A mechanism that accepts only specified parameters to enhance security ● Use flash when redirecting and flash.now when rendering ● After redirect_to, issue a new request with "URL". The completed form becomes blank After redirect, call the view with "view file name" = not through the controller. Input contents are retained

Recommended Posts

I tried Rails beginner [Chapter 1]
I tried Rails beginner [Chapter 2]
[Rails] I tried deleting the application
I tried to introduce CircleCI 2.0 to Rails app
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 7
I tried Spring.
rails tutorial Chapter 5
rails tutorial Chapter 10
I tried tomcat
rails tutorial Chapter 9
I tried youtubeDataApi.
I tried refactoring ①
rails tutorial Chapter 8
I tried FizzBuzz.
[Beginner] Rails Tutorial
I tried JHipster 5.1
I tried to organize the session in Rails
I tried writing CRUD with Rails + Vue + devise_token_auth
[Ruby basics] I tried to learn modules (Chapter 1)
[I tried] Spring tutorial
Chapter 4 Rails Flavored Ruby
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
I tried running Autoware
I tried using Gson
Rails Tutorial Chapter 3 Notes
I tried QUARKUS immediately
Rails Tutorial Chapter 3 Learning
I tried using TestNG
I tried Spring Batch
I tried using Galasa
Rails Tutorial Memorandum (Chapter 3, 3.1)
I tried node-jt400 (Programs)
Rails Tutorial Chapter 4 Notes
I tried node-jt400 (execute)
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
[Beginner] About Rails Session
I tried node-jt400 (Transactions)
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
[Rails] I tried playing with the comment send button
[Beginner] I stumbled upon launching a project with Rails6
I introduced Docker to Rails 6, so I summarized it (beginner)
Rails6 I tried to introduce Docker to an existing application
I tried to build an environment using Docker (beginner)
I tried unit testing Rails app using RSpec and FactoryBot
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I started Java Gold (Chapter 1-1)
I tried DI with Ruby
I tried node-jt400 (IFS write)
[Rails] I tried using the button_to method for the first time
[Rails] I tried to create a mini app with FullCalendar
I tried node-jt400 (SQL Update)
I tried using azure cloud-init
I tried Spring State machine
I tried Drools (Java, InputStream)
I tried using Hotwire to make Rails 6.1 scaffold a SPA