This time is a continuation of the previous article.
Please see the previous article if you like.
Explanation of Ruby on rails for beginners ①
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Explanation of Ruby on rails for beginners ④ ~ How to use naming convention and form_Tag ~
Let's get the data of the users table and display it with the following code.
index.html.erb
<% @users.each do |user| %>
<p>name</p>
<div class="ruby-name"><%= user.name %></div>
<% end %>
I'm passing it in the controller to use the instance variable @users.
users_controller.rb
def index
@users = User.all
end
The following screen will be displayed.
In addition, css is specified as follows.
users.scss
.ruby-name {
color: purple;
display: inline-block;
font-weight: bold;
}
Now let's implement database deletion.
Rewrite the code as follows.
index.html.erb
<% @users.each do |user| %>
<p>name</p>
<div class="ruby-name">
<%= user.name %>
<%= link_to("Delete", "/users/#{user.id}/destroy", {method: "post"})%>
</div>
<% end %>
I was able to delete the element by clicking the delete button.
Let's look at the specific processing. The following code has been added to index.html.erb.
<%= link_to("Delete", "/users/#{user.id}/destroy", {method: "post"})%>
This will cause the second argument to be processed when you click Delete.
The second argument "/users/#{user.id}/destroy" includes the database id in the routing to the destroy action of the users controller. By specifying the URL in this way, the controller can receive the id of the database you want to delete.
The third argument specifies that this is a post request, not a get request.
Please route as follows.
routes.rb
post "users/:id/destroy" => "users#destroy"
The: id part can accept any number. The received number is stored in params [: id]
in the users controller.
Code your controller as follows:
users_controller.rb
def destroy
user = User.find_by(id: params[:id])
user.destroy
redirect_to("/users/index")
end
In the part of ʻuser = User.find_by (id: params [: id])`, the data is extracted from the database using the model. Extract the data with the same id as the id sent from index.html.erb from the database and store it in user.
ʻThe data is deleted in the part of user.destroy`.
At the redirect_to ("/ users / index ")
part, we are redirecting to index.html.erb. This time, it will be reloaded because it is the operation when the delete link is clicked from index.html.erb.
At this point, you have successfully deleted the data from the database.
Next, let's edit the database.
Edit ʻindex.html.erb` as follows:
index.html.erb
<% @users.each do |user| %>
<p>name</p>
<div class="ruby-name">
<%= user.name %>
<%= link_to("Delete", "/users/#{user.id}/destroy", {method: "post"})%>
<%= link_to("Edit", "/users/#{user.id}/edit") %>
</div>
<% end %>
Newly added this time is the <% = link_to ("edit "," / users / # {user.id} / edit ")%>
part.
This will take you to a new view file called ʻusers / edit`.
At that time, you will pass the id of the database you want to edit to the view file.
Route as follows:
routes.rb
get "users/:id/edit" => "users#edit"
Prepare the following view file in the following path.
edit.html.erb
<%= form_tag("/users/#{@user.id}/update") do %>
<input type="text" value="<%[email protected]%>" name="new_name">
<input type="submit" value="Send">
<% end %>
As a test, click the part that says Edit index.html.erb file below.
It will be as follows.
Here is a description of the edit.html.erb file.
The form_tag ("/ users / # {@ user.id} / update ")
part specifies which action of which controller to use.
This time we will use the update action of the users controller. Also, to edit the database, send the id of the database you want to edit. form_tag was a post request.
In the part of <input type =" text "value =" <% [email protected]%> "name =" new_name ">
, the initial value and name of the input tag are set. @user
is sent from the edit action of the users controller.
users_controller.rb
def edit
@user = User.find_by(id: params[:id])
end
@ User
contains the value found in the database according to id.
Now, let's change the value as shown below and send it.
Then, the following routing will execute the update action of the users controller.
routes.rb
post "users/:id/update" => "users#update"
users_controller.rb
def update
user = User.find_by(id: params[:id])
user.name = params[:new_name]
user.save
redirect_to("/users/index")
end
In the update action, search the database according to the sent id, store it in the local variable user, rewrite the data in the name column to the new name sent, and save it with ʻuser.save. After that, I'm redirecting to
/ users / index`.
Therefore, it changes as follows.
This is the end of this article.
Thank you for your relationship.
Please see the following articles if you like.
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ⑦ ~ Implementation of flash ~
Recommended Posts