27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

20 CHAPTER 1 Ruby on Rails, the framework<br />

As you can see by this example, the validations you defined in your Purchase model<br />

take effect for both the creation and updating of records automatically.<br />

Now what would happen if, rather than update a purchase, you wanted to delete<br />

it? That’s built in to the scaffold too.<br />

1.2.11 Deleting<br />

In Rails, delete is given a much more forceful name: destroy. This is another sensible<br />

name because to destroy a record is to put an end to its existence. 16 Once this record’s<br />

gone, it’s gone, baby, gone.<br />

You can destroy a record by going to http://localhost:3000/purchases and clicking<br />

the Destroy link shown in figure 1.11 and then clicking OK on the confirmation<br />

box that pops up.<br />

When that record’s destroyed, you’re taken back to the Listing Purchases page.<br />

You’ll see that the record no longer exists. You should now only have one record, as<br />

shown in figure 1.12.<br />

How does all of this work? Let’s look at the index template in the following listing<br />

to understand, specifically the part that’s used to list the purchases.<br />

<br />

<br />

<br />

<br />

<br />

<br />

'Are you sure?',<br />

:method => :delete %><br />

<br />

<br />

<br />

<br />

<br />

16 Mac OS X dictionary.<br />

Listing 1.16 app/views/purchases/index.html.erb<br />

Figure 1.11 Destroy! Figure 1.12 Last record standing

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!