--People who have just started learning Rails --People trying to make their first app with Rails --People who are used to Rails but want to review a little
This is an article that the author, who is a beginner of Rails, lists what he learned in the Rails course of progate that he completed the other day. Since it is supplemented with information from other articles in some places, there are some differences from the content expressed and explained by progate management.
A computer that provides data at the request of a client (browser). In order to display the Rails-made app on the browser, you need to start this server.
An HTML file to create the look of the page. In the interaction between the browser and Rails, Rails returns a view to display the page.
The extension is .html.erb
. erb is an abbreviation for Embedded Ruby, a mechanism for embedding Ruby scripts in HTML files.
One of the three elements needed to create a page, similar to the controller routing below.
For returning a view to the browser. Each controller is created according to the purpose. For example, post-related functions and user-related functions are specified in separate controller files.
A feature in the controller to find the view to return to the browser. Expressed as a method in the controller file.
It's like a correspondence table that decides which controller and which action to process the sent URL.
A place to store your data.
A table that manages data in the database. The data for each row is called a record, and the data for each column is called a column.
A class that interacts with the database. Data can be stored in a table by creating an instance of the model and saving it.
A file that tells the database to make changes.
<% %>
ex)<%= @post %>
When embedding Ruby code in an HTML file, write it before and after the code. If you want the code to be displayed in the browser, write <% =%>
before and after the code.
<%= link_to("Character to display", "URL") %>
ex) <%= link_to("Post list", "/posts/index")%>
Create a link. Used in HTML files. By adding {method: "post"} to the third argument, it will match the routing defined as post.
form_tag
<%= form_tag("URL") do %>
data
<% end %>
ex)
<%= form_tag("/posts/update") do %>
<textarea name="content"><%= @post.content %><textarea>
<input type="submit" value="Post">
<% end %>
The data entered in the form can be sent to the specified URL. However, form_tag alone is meaningless, and by specifying the name attribute in the textarea tag (or input tag), the hash with the name attribute as the key can be transmitted to the action side. As an aside, by putting a value between the textarea tags as described above, you can enter from the continuation of the content that was originally entered when re-entering the form.
errors.full_messages
<%Instance name.errors.full_messages.each do |message| %>
<%= message %>
<% end %>
ex)
<% @posts.errors.full_messages.each do |message| %>
<%= message %>
<% end %>
Output an error message. If validation fails when you call the save method, Rails will automatically generate an error message, so you can display everything by using the each statement.
yield
html:views/layout/application.html.erb
<%= yield %>
Each view file is assigned to the yield described in application.html.erb. application.html.erb is a view file that describes the layout that applies to the entire site.
@post = "hogehoge"
If you define it in the controller file, you can use it when embedding Ruby code in the view file.
Model name.new(Column name:value)
ex) post = Post.new(content: "hogehoge")
Create an instance from the model.
Instance name.save
ex) post.save
Save the created instance in the table.
Model name.all
ex) posts = Post.all
Get all the records in the table.
Model name.find_by(Column name:value)
ex) post = Post.find_by(id: 1)
Get one data that matches a certain condition.
Model name.where(Column name:value)
ex) posts = Post.where(id: 1)
Acquire multiple data that meet a certain condition.
redirect_to("URL")
ex) redirect_to("/posts/index")
You can transfer to the specified page.
render("Folder name/file name")
ex) render("posts/edit")
You can view the view directly without going through another action. It is often used when data saving fails.
Model name.order(Column name: :Sorting order)
ex) @posts = Post.all.order(created_at: :desc)
Sort the acquired data. : desc represents descending order and: asc represents ascending order.
Instance name.destroy
ex) post.destroy
Deletes the specified data (instance) from the database.
session[:Key name] =value
ex) session[:user_id] = @user.id
This is to keep the logged-in user's information even when the page is moved. You can prevent you from being logged in by substituting nil.
params
ex1) @id = params[:id]
ex2) @post = Post.new(content: params[:content])
Example 1. Get the value of: ○○ of the URL set in the routing. Example 2. Receive the input contents of the form with name = "○○".
before_action
before_action Common processing for all actions
ex) before_action :set_current_user, {only: [:edit, :update]}
The described process is always executed before calling any action. Write at the top of the file. By using {only: [: action name]}, it is possible to limit the action to be executed.
app name/config/routes.rb
get "URL" => "Controller name#Action name"
ex) get "/posts/index" => "posts#index"
Get the specified information. If you don't want to change the database, use get. get and the post below are called HTTP methods.
app name/config/routes.rb
post "URL" => "Controller name#Action name"
ex) post "/posts/create" => "posts#create"
The method used to change the value of session when changing the database. It seems that there are many other HTTP methods, but I haven't learned them yet, so I'll omit them here.
app name/config/routes.rb
get "posts/:id" => "posts#show"
If you put a string starting with: in the URL part of the routing, that string will be recognized as a parameter. Therefore, in this example, all URLs such as / posts / ○○ can be directed to the show action. For that reason, routings like / posts / index must be written before it so that they don't get caught in the routing for / posts /: id.
validates
validates :Column name to verify, {Contents to be verified}
ex) validates :content, {presence: true}
Check the data (validation) so that malicious data is not stored in the database. The main verification contents are as follows.
Verification content | meaning |
---|---|
presecse: true | Check if the value in that column exists |
length: {maximum:word count} | Prevent data from saving more than the specified number of characters |
uniqueness: true | Check if duplicate data exists in the database |
add_column :table name, :Column name, :Data type
ex) add_column :users, :image_name, :string
Add a column to an existing table. Write in the change method.
--rails new app name
A folder with the same name as the entered application name will be created, and the folders and files required for development will be prepared in it.
rails server
Start the server.
--rails g controller controller name action name
Creating a controller. g can be generate.
The controller name and action name are often the same as the HTTP method (URL).
--rails g model model name column name: data type
Creating migration and model files.
The model name is singular and the first letter is capitalized.
--rails g migration filename
Create only the migration file.
The file name should be something descriptive, such as add_image_name_to_users.
rails db:migrate
Reflection of migration file to database.
I created a migration file, but if I don't run it, I get an error.
progate is good.
-[Introduction] What is a server? Easy-to-understand explanation of roles and types! -View Basics -Thorough explanation of Rails model! Introducing three things you need to know -Node.js + Express URL routing
-[Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field](https://www.amazon.co.jp/%E7%8F%BE%E5%A0%B4%E3%81%A7%E4%BD% BF% E3% 81% 88% E3% 82% 8B-Ruby-Rails-5% E9% 80% 9F% E7% BF% 92% E5% AE% 9F% E8% B7% B5% E3% 82% AC% E3% 82% A4% E3% 83% 89-% E5% A4% A7% E5% A0% B4-ebook / dp / B07JHQ9B5T)
Recommended Posts