[Ruby] Rails CRUD function implementation ② (this time, edit, details)

2 minute read

Continuing from the previous time, edit and details will be described.

Procedure for adding functions Review: routes.rb → controller addition → view addition

Editing function

The procedure is similar to a new post. Edit screen display (edit) → Update with input contents (update)

routes.rb


resources :birds, only: [:index, :new, :create, :destroy, :edit]

image.png

Paste the link to the edit screen on the list screen. The Prefix is “edit_bird” and the id is passed, and since it is a GET method, it will be as follows.

index.html.erb


<%=link_to "edit", edit_bird_path(bird.id), method: :get %>

Get the record that matches the passed id and pass it to the edit screen.

birds_controller.rb


def edit
  @bird = Bird.find(params[:id])
end

And the edit screen.

edit.html.erb


<%=form_with(model:@bird, local:true) do |form|%>
  <%=form.text_field :name, placeholder: "bird name"%>
  <%=form.text_field :pic, placeholder: "bird photo URL"%>
  <%= form.submit "update"%>
<%end%>

The link to the editing screen is displayed properly. image.png

If you click it, it will transition properly! image.png

The update process starts from here!

First of all, routes.rb!

routes.rb


resources :birds, only: [:index, :new, :create, :destroy, :edit, :update]

Update the DB with the input received by the controller! Use the bird_param created in the new post!

birds_controller.rb


...
  def update
    bird = Bird.find(params[:id])
    bird.update(bird_params)
  end
  private
  def bird_param
# params.require(:model name).permit(:column name,:column name,......)
    params.require(:bird).permit(:name, :pic)
  end
...

image.png

image.png

image.png

It has been properly changed to Kingfisher 2!

Detailed display function

This is the last feature! let’s do our best! !!

routes.rb


resources :birds, only: [:index, :new, :create, :destroy, :edit, :update, :show]

However, since all the functions are added, the description of only is unnecessary! Let’s erase it!

routes.rb


  resources :birds

It was pretty refreshing! !!

Let’s put a link to the detail screen on the list screen! When you run the rails routes command image.png The prefix is bird, the method is GET, and let’s do the id!

index.html.erb


<%= link_to'details', bird_path(bird.id), method: :get %>

Then the controller receives the id and gets the record!

birds_controller.rb


def show
    @bird = Bird.find(params[:id])
  end

Finally, it’s show.html.erb!

show.html.erb


<%= @bird.name %>
<div style=
"background-image: url(<%= @bird.pic %>);
 background-position: center center;
 background-size: cover;
 width: 300px;
 height: 300px;
 margin-bottom: 10px;
 ">
</div>

image.png

The one you have selected properly is displayed.

Summary

Rails will automatically identify the link destination if you pass the model, if you understand the rules of the framework, you can make a basic site with explosive speed. This time, we will implement the login function, so please look forward to it! Thank you until the end! !!