・ 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
[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).
[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
An environment where you can check rails code interactively about db processing
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
○変数.save ⇨ This completes the overwrite process
<h3> [About rails console commands (deleted version)] </ h3>
** ○ 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