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.

Deleting projects<br />

Listing 4.11 features/editing_projects.feature<br />

Scenario: Updating a project with invalid attributes is bad<br />

And I fill in "Name" with ""<br />

And I press "Update Project"<br />

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

When you run bin/cucumber features/editing_projects.feature, the first step<br />

passes but the second doesn’t:<br />

expected there to be content "Project has not been updated." in "[text]"<br />

Again, this error means that it was unable to find the text “Project has not been<br />

updated.” on the page. This is because you haven’t written any code to test for what to<br />

do if the project being updated is now invalid. In your controller, use the code in the<br />

following listing for the update action.<br />

Listing 4.12 app/controllers/projects_controller.rb<br />

def update<br />

@project = Project.find(params[:id])<br />

if @project.update_attributes(params[:project])<br />

flash[:notice] = "Project has been updated."<br />

redirect_to @project<br />

else<br />

flash[:alert] = "Project has not been updated."<br />

render :action => "edit"<br />

end<br />

end<br />

And now you can see that the feature passes when you rerun it:<br />

2 scenarios (2 passed)<br />

15 steps (15 passed)<br />

Again, you should ensure everything else is still working by running rake cucumber<br />

:ok; you should see this summary:<br />

5 scenarios (5 passed)<br />

31 steps (31 passed)<br />

Let’s make a commit and push now:<br />

git add .<br />

git commit -m "Added updating projects functionality"<br />

git push<br />

The third part of CRUD, updating, is done. The fourth and final part is deleting.<br />

4.3 Deleting projects<br />

We’ve reached the final stage of CRUD: deletion. This involves implementing the final<br />

action of your controller, the destroy action, which allows you to delete projects.<br />

91

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

Saved successfully!

Ooh no, something went wrong!