routes.rb
resources :birds, only: [:index, :new, :create, :destroy, :edit]
Paste the link to the edit screen on the list screen. Prefix passes the id with "edit_bird" and it is a GET method, so 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 edit screen is displayed properly.
If you click it, it will transition properly!
routes.rb
resources :birds, only: [:index, :new, :create, :destroy, :edit, :update]
The controller receives the input contents and updates the DB! Use 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
...
It has been properly changed to Kingfisher 2!
routes.rb
resources :birds, only: [:index, :new, :create, :destroy, :edit, :update, :show]
It's okay, but since all the functions have been added, you don't need to write only! Let's erase it!
routes.rb
resources :birds
It was pretty refreshing! !!
Paste the link to the details 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 too!
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, 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>
The one you have selected is displayed.
Recommended Posts