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.

Creating tickets<br />

ticket URL is always scoped by /projects/:id in your application. This helper and its<br />

brethren are defined by changing this line in config/routes.rb<br />

resources :projects<br />

to the lines in the following listing.<br />

Listing 5.3 config/routes.rb<br />

resources :projects do<br />

resources :tickets<br />

end<br />

This code tells the routing for Rails that you have a tickets resource nested inside the<br />

projects resource. Effectively, any time you access a ticket resource, you access it within<br />

the scope of a project too. Just as the resources :projects method gave you helpers to<br />

use in controllers and views, this nested one gives you the helpers (where id represents<br />

the identifier of a resource) shown in table 5.1.<br />

Table 5.1 Nested RESTful routing matchup<br />

As before, you can use the *_url alternatives to these helpers, such as project<br />

_tickets_url, to get the full URL if you so desire. The :project_id symbol here<br />

would normally be replaced by the project ID as well as the :id symbol, which would<br />

be replaced by a ticket’s ID.<br />

In the left column are the routes that can be accessed, and in the right, the routing<br />

helper methods you can use to access them. Let’s make use of them by first creating<br />

your TicketsController.<br />

5.1.2 Creating a tickets controller<br />

Route Helper<br />

/projects/:project_id/tickets project_tickets_path<br />

/projects/:project_id/tickets/new new_project_ticket_path<br />

/projects/:project_id/tickets/:id/edit edit_project_ticket_path<br />

/projects/:project_id/tickets/:id project_ticket_path<br />

Because you defined this route in your routes file, Capybara can now click the link in<br />

your feature and proceed before complaining about the missing TicketsController,<br />

spitting out an error followed by a stack trace:<br />

And I follow "New Ticket"<br />

uninitialized constant TicketsController ...<br />

Some guides may have you generate the model before you generate the controller,<br />

but the order in which you create them is not important. In Cucumber, you just<br />

follow the bouncing ball, and if Cucumber tells you it can’t find a controller, then<br />

you generate the controller it’s looking for next. Later, when you inevitably receive<br />

101

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

Saved successfully!

Ooh no, something went wrong!