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.
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 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
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
-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
● Creating a model (interacting with the database) rails g model model name
t. Type name: Column name
rails db: migrate
● 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.
● Define and route show actions, create view files
● Routing when accessing localhost: 3000: root to:" URL "
This is the end of Chapter 1.
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