27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

88 CHAPTER 4 Oh CRUD!<br />

4.2 Editing projects<br />

With the first two parts of CRUD (creating and reading) done, you’re ready for the<br />

third part: updating. Updating is similar to creating and reading in that it has two<br />

actions for each part (creation has new and create, reading has index and show). The<br />

two actions for updating are edit and update. Let’s begin by writing a feature and creating<br />

the edit action.<br />

4.2.1 The edit action<br />

As with the form used for creating new projects, you want a form that allows users to<br />

edit the information of a project that already exists. You first put an Edit Project link<br />

on the show page that takes users to the edit action where they can edit the project.<br />

Write the feature from the following listing into features/editing_projects.feature.<br />

Listing 4.5 features/editing_projects.feature<br />

Feature: Editing Projects<br />

In order to update project information<br />

As a user<br />

I want to be able to do that through an interface<br />

Scenario: Updating a project<br />

Given there is a project called "TextMate 2"<br />

And I am on the homepage<br />

When I follow "TextMate 2"<br />

And I follow "Edit Project"<br />

And I fill in "Name" with "TextMate 2 beta"<br />

And I press "Update Project"<br />

Then I should see "Project has been updated."<br />

Then I should be on the project page for "TextMate 2 beta"<br />

In this story, you again use the bin/cucumber command to run just this one feature:<br />

bin/cucumber features/editing_projects.feature.<br />

The first three steps pass for this feature because of the work you’ve already done,<br />

but it fails on the fourth step when it tries to follow the as-yet nonexistent Edit Project<br />

link on the show page:<br />

no link with title, id or text 'Edit Project' found (Capybara::ElementNotFound)<br />

To add this link, open app/views/projects/show.html.erb and add the link from the<br />

following listing underneath the code currently in that file.<br />

Listing 4.6 app/views/projects/show.html.erb<br />

<br />

The edit_project_path method generates the link to the Project object, pointing at<br />

the ProjectsController’s edit action. This method is provided to you because of the<br />

resources :projects line in config/routes.rb.

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

Saved successfully!

Ooh no, something went wrong!