I learned rails
with progate and dot installation and explained the code used.
It will be used as an output to improve the efficiency of learning, so I would be grateful if you could point out any mistakes.
$ rails new myblog
Create an application.
$ rails g model Post title:string body:text
Create a model and table.
A model is a class that interacts with the database.
string
creates a string and text
creates a long string.
$ rails db:migrate
Reflect changes in the database
$ rails g controller Posts
Create a controller
.
controller
: It has the role of receiving requests from the browser and exchanging with model / view.
$ rails routes
Check the contents of the set routing. Routing: Recognizes the received URL and assigns it to the appropriate in-controller action.
$ rails c
Run rails console
$ rails s
Start the rails server.
@post = Post.find_by(id: params[:id])
Find the appropriate id in the database and assign that data to @ post
.
@post.save
Save the contents of @ post
to the database.
@post.destroy
Delete the contents of @ post
from the database.
When writing a link to a destroy
action, write a method.
def update
redirect_to("Folder name/file name")
end
redirect_to
makes a page transition to the specified URL after executing the action.
def update
render("Folder name/file name")
end
render
displays the view directly at the specified URL without going through another action.
def update
flash[:notice] = "Enter the characters you want to display"
end
Display a flash message in the place where <% = flash [: notice]%>
is described.
The feature of flash messages is that they are displayed only once on the page.
def index
@posts = Post.all
end
Get all the data.
Let's take a look at the steps to create a detail page as an example.
Therefore, the action name is show
.
The controller name is posts
described in the command line item.
There are three steps, but it doesn't matter which one you start with.
posts_controller.rb
def show
end
① Add the show
action to the controller
file.
routes.rb
get "posts/:id" => "posts#show"
② Add routing to the show
action.
③ Create show.html.erb
in the ʻapp →
views→
posts` folder in the created application.
You will see a link that takes you to the details page from ʻindex`.
erb:index.html.erb
<%= link_to(post.content, "/posts/#{post.id}") %>
The link_to
method corresponds to the<a>
tag.
In this case, the text described in post
becomes a link and moves to the page of ʻid` obtained from the database.
erb:new.html.erb
<%= form_tag("/posts/create") do %>
<textarea name="content"></textarea>
<% end %>
form_tag
sends the value of the input form.
Set the name
attribute to specify the value to send.
posts_contoroller.rb
@post = Post.new(content: params[:content])
@post.save
redirect_to("/posts/index")
Save the data received from the input form in the database. After saving, go to the ʻindex.html.erb` page.
Terminal
rails g migration filename
① Create a migration file.
def
add_column :table name, :Column name, :Data name
(2) Describe the contents of the migration file.
Terminal
rails bg:migrate
③ Save in the database.
Anyway, I would like to continue learning to the extent that I can make deliverables with rails
!
Recommended Posts