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.

256 CHAPTER 10 Tracking state<br />

The first option tag in the select tag has an additional attribute: selected. When<br />

this attribute is set, the option is the one selected as the default option for the select.<br />

This is achieved by using the :selected option for f.select. The value for this<br />

option is the corresponding value attribute for the option tag. In this case, it’s the<br />

state_id of the @ticket object.<br />

With the select box in place, you’re almost at a point where this scenario will be<br />

passing. Let’s see how far you’ve gotten by running bin/cucumber features/creating<br />

_comments.feature. It won’t be able to find the Open option in your select box:<br />

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

No such option 'Open' in this select box. Available options:<br />

➥(Capybara::OptionNotFound)<br />

This is because you need to add a state to your database! Let’s add this line to the bottom<br />

of your Background in features/creating_comments.feature to do this:<br />

Given there is a state called "Open"<br />

Let’s now run the scenario using bin/cucumber features/creating_comments<br />

.feature so you can get the step definition. Put this step definition in a new file called<br />

features/step_definitions/state_steps.rb using this code:<br />

Given /^there is a state called "([^"]*)"$/ do |name|<br />

State.create!(:name => name)<br />

end<br />

By defining this step and rerunning the scenario you’ll see that it’s now failing at the<br />

last step:<br />

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

Unable to find css "#ticket .state" (Capybara::ElementNotFound)<br />

This output means it’s looking for any element with the id attribute of ticket that<br />

contains any type of element with the id of state, but it can’t find it.<br />

Rather than putting the state inside the TicketsController’s show template, put it<br />

in a partial. This is due to the fact that you’ll be reusing this to display a state wherever<br />

you need it in the future. Additionally, you’ll apply a dynamic class around the state so<br />

you can style it later on. Let’s create a new partial at app/views/states/_state.html.erb<br />

and fill it with this content:<br />

<br />

<br />

<br />

To style the element you need a valid CSS class name. You can get one by using the<br />

parameterize method. If, for example, you had a state called “Drop bears strike<br />

without warning!” and used parameterize on it, all the spaces and non-URL-valid<br />

characters would be stripped, leaving you with “drop-bears-strike-without-warning,”<br />

which is a perfectly valid CSS class name. You’ll use this later on to style the state using<br />

the color and background attributes.

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

Saved successfully!

Ooh no, something went wrong!