CRUD Application: Part 3
Explore how to implement full CRUD operations in Ruby on Rails by learning to update and delete pet records. Understand editing views, creating update and destroy controller actions, and adding links for editing and removal in index and show pages.
So far, your app allows you to perform the C and R of the CRUD operations. We can look at existing pets, retrieve their details, and create new ones.
Next, you will learn how to perform the U and D on our app. Here are the steps you will follow:
- Edit the
editaction inapp/controllers/pets_controller.rbto take you to the edit view - Edit
edit.html.erbto display the form to edit existing pets.3. Create anupdatemethod inapp/controllers/pets_controller.rbto handle form updates - Edit
index.html.erbandshow.html.erbto display Edit links - Create a
destroymethod to handle pet removal - Edit
index.html.erbandshow.html.erbto display Remove links
Updating pets
Edit the edit action
You will modify your edit action in the app/controllers/pets_controller.rb file by adding the following line:
@pet = Pet.find(params[:id])
This is the same as what you did for your show action.