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.

114 CHAPTER 5 Nested resources<br />

Again, you’re defining the action here so that anybody coming through and reading<br />

your TicketsController class knows that this controller responds to this action. It’s<br />

the first place people will go to determine what the controller does, because it is the<br />

controller.<br />

The next logical step is to create the view for this action. Put it at app/views/<br />

tickets/edit.html.erb and fill it with this content:<br />

Editing a ticket in <br />

<br />

Here you re-use the form partial you created for the new action, which is handy. The<br />

form_for knows which action to go to. If you run the feature command here, you’re<br />

told the update action is missing:<br />

And I press "Update"<br />

The action 'update' could not be found TicketsController<br />

5.3.2 Adding the update action<br />

You should now define the update action in your TicketsController, as shown in the<br />

following listing.<br />

Listing 5.14 app/controllers/tickets_controller.rb<br />

def update<br />

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

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

redirect_to [@project, @ticket]<br />

else<br />

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

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

end<br />

end<br />

Remember that in this action you don’t have to find the @ticket or @project objects<br />

because a before_filter does it for the show, edit, update, and destroy actions. With<br />

this single action implemented, both scenarios in your ticket-editing feature pass:<br />

2 scenarios (2 passed)<br />

20 steps (20 passed)<br />

Now check to see if everything works:<br />

12 scenarios (12 passed)<br />

100 steps (100 passed)<br />

# and<br />

5 examples, 0 failures, 4 pending<br />

Great! Let’s commit and push that:<br />

git add .<br />

git commit -m "Implemented edit action for the tickets controller"<br />

git push

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

Saved successfully!

Ooh no, something went wrong!