Learn
Associations I
Update a destination II
Nicely done! You can now update a destination’s name and description. How does it work?
When you visit http://localhost:8000/destinations/1/edit
to edit a destination, it triggers the first turn of the request/response cycle:
- The browser makes a HTTP GET request for the URL
/destinations/1/edit
. - The Rails router maps this URL to the Destinations controller’s
edit
action. Theedit
action finds the destination with id 1, stores it in@destination
, and passes it on to the view app/views/destinations/edit.html.erb. - In the view,
form_for
creates a form with the fields of the@destinations
object.
Then when you fill out the form and submit it, it triggers the second turn of the request/response cycle:
- The browser sends the data to the Rails app via an HTTP POST request to the URL
/destinations/update
. - This time, the Rails router maps this URL to the
update
action. - The
update
uses thedestination_params
method to safely collect data from the form. It finds the destination in the database, updates its attributes, and redirects to the destination’sshow
page.
Instructions
Click Next to continue.