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.

The projects API<br />

last_response.status.should eql(200)<br />

end<br />

end<br />

When you run bin/rspec spec/v1/api/projects_spec.rb, this spec will fail because<br />

the destroy action doesn’t exist:<br />

1) /api/v1/projects deleting a project JSON<br />

Failure/Error: delete "#{url}.json", :token => token<br />

The action 'destroy' could not be found for Api::V1::ProjectsController<br />

You need to add the destroy action to the Api::V1::ProjectsController, which you<br />

can do with this code:<br />

def destroy<br />

@project.destroy<br />

respond_with(@project)<br />

end<br />

The respond_with here will respond with a 200 status code and an empty JSON or<br />

XML response, which indicates the object was successfully destroyed. But where does<br />

@project come from? Your before_filter should set this up, but it doesn’t right now.<br />

Let’s fix it by changing it from this<br />

before_filter :find_project, :only => [:show,<br />

:update]<br />

to this:<br />

before_filter :find_project, :only => [:show,<br />

:update,<br />

:destroy]<br />

When you run bin/rspec spec/api/v1/projects_spec.rb, does it pass?<br />

8 examples, 0 failures<br />

It does, because you’re great at what you do! That’s the final piece of the projects API,<br />

and now people are able to create, read, update, and delete projects through it. The<br />

rest of your specs probably pass because you didn’t change anything outside the<br />

scope, but it’s still good to do a check. For old time’s sake, you’ll also run the features<br />

to make sure everything’s ok there too. The full command that you’ll now run is rake<br />

cucumber:ok spec. For this command, you should see this output:<br />

63 scenarios (63 passed)<br />

732 steps (732 passed)<br />

# and<br />

52 examples, 0 failures, 19 pending<br />

All systems are go, so let’s make a commit at this lovely point in time where everything<br />

is beautiful:<br />

git add .<br />

git commit -m "Projects can now be deleted through the API"<br />

373

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

Saved successfully!

Ooh no, something went wrong!