Continuing from the previous time, edit and details will be described.
Procedure for adding functions Review: routes.rb → controller addition → view addition
The procedure is similar to a new post. Edit screen display (edit) → Update with input contents (update)
resources :birds, only: [:index, :new, :create, :destroy, :edit]
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.
<%=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.
def edit @bird = Bird.find(params[:id]) end
And the edit screen.
<%=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.
If you click it, it will transition properly!
The update process starts from here!
First of all, 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!
... 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 ...
It has been properly changed to Kingfisher 2!
Detailed display function
This is the last feature! let’s do our best! !!
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!
It was pretty refreshing! !!
Let’s put a link to the detail screen on the list screen! When you run the rails routes command The prefix is bird, the method is GET, and let’s do the id!
<%= link_to'details', bird_path(bird.id), method: :get %>
Then the controller receives the id and gets the record!
def show @bird = Bird.find(params[:id]) end
Finally, it’s 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>
The one you have selected properly is displayed.
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! !!