Goal

You’ll understand the full set of seven standard RESTful actions Rails expects for a resource, and be able to explain what genuinely distinguishes “new” from “create,” and “edit” from “update” — pairs beginners often confuse.

Learn

The resources :products shortcut from Part 1.2 generates seven standard actions, each with a specific, distinct purpose:

Action HTTP Verb Purpose
index GET list all records
show GET display one record
new GET display a blank form for creating a record
create POST actually save a new record, submitted from the new form
edit GET display a pre-filled form for editing an existing record
update PATCH/PUT actually save changes, submitted from the edit form
destroy DELETE delete a record

The genuinely important distinction, easy to confuse at first: new and edit only display a form — they don’t change any data at all. create and update are what actually persist data to the database, triggered specifically when that form is submitted. This is exactly why they come in pairs: new renders the form that, when submitted, hits create; edit renders the form that, when submitted, hits update.

This pairing also explains the different HTTP verbs: new and edit use GET (just displaying a page, nothing changes), while create and update use POST/PATCH (actually submitting data that changes something) — directly connecting to the GET-vs-POST reasoning from earlier web fundamentals.

Decision Task

A user visits a Rails app’s “edit product” page, sees the form, but closes the browser tab without clicking submit. Before reading on: has any actual data been changed in the database at this point?

Show Answer

No — visiting the edit page only triggers the edit action, which just displays a pre-filled form; no data changes at all. Data would only actually change if the form were submitted, triggering the separate update action. This is exactly the important distinction this lesson covers: edit displays, update persists.

Common Mistake

Confusing new/edit (which only display forms, using GET) with create/update (which actually persist data, using POST/PATCH), sometimes leading to putting data-modifying logic inside the wrong action. new and edit should genuinely just prepare and display a form; the actual database changes belong specifically in create and update, triggered only when that form is actually submitted.

Practice Questions

1. What HTTP verb does the new action use, and why does that make sense given what it actually does?

Show Answer

GET — since new only displays a form and doesn’t change any data, matching GET’s meaning as a safe, non-data-changing request.

2. What HTTP verb does the create action use, and why?

Show Answer

POST — since create actually persists new data submitted from a form, matching POST’s meaning as a data-changing request.

3. What’s the genuine functional difference between edit and update?

Show Answer

edit displays a pre-filled form for editing an existing record, without changing any data; update actually saves the changes when that form is submitted.

4. True or False: visiting a resource’s “new” page automatically creates a blank record in the database.

Show Answer

False — new only displays a form; no record is created until the form is actually submitted, triggering the separate create action.

5. Why do new/create and edit/update genuinely come in pairs, rather than being unrelated separate actions?

Show Answer

Because the form displayed by new (or edit) is specifically designed to be submitted to create (or update) — they’re two halves of one complete workflow: display the form, then handle its submission.

Try It Yourself

Without looking back, explain in your own words why “index” and “show” both use GET, connecting this to the same GET-reasoning covered for new and edit in this lesson.

Show Answer

index (listing records) and show (displaying one record) are both just retrieving and displaying existing data, with no changes made to anything — exactly matching GET’s meaning as a safe, non-data-changing request, the same reasoning that explains why new and edit also use GET.

Quick Check

1. What does the index action do?

Show Answer

Lists all records for a resource.

2. What does the new action do, and what HTTP verb does it use?

Show Answer

Displays a blank form for creating a record; uses GET.

3. What does the create action do, and what HTTP verb does it use?

Show Answer

Actually saves a new record submitted from the new form; uses POST.

4. What’s the difference between edit and update?

Show Answer

edit displays a pre-filled form; update actually saves the submitted changes.

5. How many standard RESTful actions does resources generate?

Show Answer

Seven — index, show, new, create, edit, update, destroy.

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.