Rails Review 1

Purpose of this post I will write it as minutes to make a note of Rails operation. This is the Rails version of [Ruby Review 1](https://qiita.com/naoto911/items/f46b35f84cc80f59cac3) and [Ruby Review 2](https://qiita.com/naoto911/items/3528067588e6884a80ba) that I wrote earlier. ..

Teaching materials used for learning Udemy's ["Introduction to Ruby on Rails-Learn Ruby and Rails from the Basics and Publish Web Applications to the Net"](https://www.udemy.com/course/the-ultimate-ruby-on-rails- I used bootcamp /) as a teaching material.

Operation flow in Rails Proceed with product production according to the following flow ① Create a new project ② Securing db area ③ Create controller and view ④ Creation of model and column information ⑤ Create db ⑥ Edit db ⑦ Controller operation

① Create a new project ~~~ rails _x.x.x_ new project name ~~~

・ Create a new rails project

[Explanation] ○rails x.x.x new ⇨ Fixed notation ⇨ Specify version with x.x.x ⇨ Rewrite the gemfile if it is not as specified

○ Project name ⇨ You can set the name yourself

② Securing db area ~~~ rails db: create ~~~ ・ If you do not execute this, you should not be able to execute ⑤db creation and ⑥db editing later.

③ Create controller and view ~~~ rails g controlle controller name (plural) action name ~~~ -Generate "controller" and "view" ・ Replace with view = html file and OK

[Explanation] ○rails g model  ⇨ Fixed notation

○ Controller name ⇨ Align the controller name with the model name * (Controller name = plural system, model name = singular system)

○ Action name ⇨ It is possible to write in multiple lists ⇨ Actions can be added later from the controller

[What is an action?] ・ Method in ruby -However, it is a method that is automatically executed when the access to the url is triggered.

[About the created file] ① controller file -Generated as app / controller / controller name_controller.rb -The def action name (generated in the controller of ①) is also automatically described.

② view directory and view file -Note that the view will generate two directories and files. ・ A directory called app / views / controller name is generated. -A file named action name.html.erb is generated (generated in this directory) -When multiple actions are listed, view files are generated for each of them (one directory).

④ Create model and column information ~~~ rails g model model name (singular) column information ~~~ -Generate "migration file" and "model definition file" ・ Here, replace model = class and OK ・ Column information of model can also be specified here

[Explanation] ○rails g model  ⇨ Fixed notation

○ Model name ・ Lowercase letters are acceptable in the terminal ・ The first character of the actual model is a large sentence

○ columon information ⇨ column name: Created in the order of data type

[About the created file] ① migration file ・ Generated in db / migrate -File name timestamp + create_modelname.rb -The table structure of the database (db) can be edited. ・ Do not store the value in db ・ No touch in the exercise, but column addition editing is probably possible here ・ The class name is'Create model name s'

(2) Model class definition file ・ Generated in app / models -File name .rb


⑤ Create db ~~~ rails db:migrate ~~~ ・ This is just an execution process -Input processing to reflect the instruction defined in migration to db

⑥ db edit ~~~ rails console ~~~ ・ Here, you can store, edit, and delete values in db for the first time. ・ In this exercise, I did it with rails console ・ Actually, it should be executable from view

[What is rails console?]

An environment where you can check rails code interactively about db processing

[About rails console commands (registration)]

after running rails console (also possible with rails c)

 ○ Model name.all
 ⇨ You can check the contents of the model (it will automatically execute sql operations)

 ○ Variable = model name.new
 ⇨ Instantiation

 ○ Variable .column name = value
 ⇨ Store the specified value in column

 ○変数.save
 ⇨ Reflect the contents after instance creation in the database

 ○ Model name.find (n)
 ⇨ Output the information of the nth instance in db (search for data with id = n)

 ○ Variable = model name (column name 1:'value 1', column name 2:'value 2')
 変数.save
 ⇨ How to write the above in one line

[About rails console commands (editing)] ~~~ ○ Variable = model name.find (n) ○ Variable .column name = value ⇨ It doesn't matter if the variable is the same as the variable before the change. ⇨ Prepare to overwrite by changing the value before and after the change

○変数.save ⇨ This completes the overwrite process


 <h3> [About rails console commands (deleted version)] </ h3>
○ Variable = model name.find (n) ○ Variable .destroy ○ Model name.find (n) ~~~ ---

⑦ Controller operation

** ○ What you can do ** ・ Control the flow of url · Sharing values in db ⇆ view

** ○ Pre-configured processing according to rails rules ** -The action is described by default (def index) ・ When this action is executed, it will access the specified url. -Of these, "create action" and "set url" have preset rails rules.

** ○ Processing performed by people ** -Store model values (image is an instance of class) · Now the instance variables are available (such as @users) -This instance variable can also be used in view -Values can be displayed in view via instance variables

** ○ Because db has multiple column structure ** -Store in @users as an array ・ Use each do in view -By doing this, column information can be output one by one in view (stored in user).

[Controller code]

qiita.rb


class UsersController < ApplicationController
  def index
    @users = User.all
  end
end

[View code]

ruby:qiita.html.erb


<ul>
    <% @users.each do |user| %>
        <li><%= user.id %>,<%= user.name %>,<%= user.age %></li>
    <% end %>
</ul>

[Output result]

1.naoto,24
2.naopiyo,20
3.kanopyo,27

Can display the value of db


Recommended Posts