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.

Changing a ticket’s state<br />

And I select "Open" from "State"<br />

And I press "Create Comment"<br />

Then I should see "Comment has been created."<br />

And I should see "Open" within "#ticket .state"<br />

In this scenario, you follow a ticket’s link from the homepage (which you go to in the<br />

Background) and go through the process of creating a comment, much like in the<br />

Creating Comments feature, only this time you select a state. This is the first part of<br />

the scenario that you can expect to fail because you don’t have a state-select box yet.<br />

After the comment is created, you should see the state appear in the #ticket .state<br />

area. This is the second part of the scenario that will fail.<br />

When you run this scenario by running bin/cucumber features/creating<br />

_comments.feature:36, it will fail like this:<br />

And I select "Open" from "State"<br />

cannot select option, no select box with id,<br />

name, or label 'State' found (Capybara::ElementNotFound)<br />

As you can see from this output, the “I select” step attempts to select an option from a<br />

select box. In this case, it can’t find the select box because you haven’t added it yet!<br />

With this select box, users of your application should be able to change the ticket’s<br />

state by selecting a value from it, entering some comment text, and clicking the<br />

Create Comment button.<br />

Before you do all that, however, you need to create the State model and its related<br />

table, which is used to store the states.<br />

10.2.1 Creating the State model<br />

Right now you need to add a select box.<br />

When you’re done, you should have one<br />

that looks like figure 10.5.<br />

Before adding this select box, let’s set up<br />

the TicketsController’s show action to<br />

return a collection of states that you can<br />

populate the drop select box with. Change<br />

the show action to be like this now:<br />

def show<br />

@comment = @ticket.comments.build<br />

@states = State.all<br />

end<br />

Here you call all on the State class, which<br />

doesn’t exist yet. You’ll be storing the states in<br />

a table because you’d like the users to be able<br />

to create their own states. For now, you define<br />

this State model to have a name field as well<br />

as two other fields: color and background, Figure 10.5 State select box<br />

253

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

Saved successfully!

Ooh no, something went wrong!