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.

Managing states<br />

Right, so you need to create the create action too, which you define inside<br />

Admin::StatesController as shown in the following listing.<br />

Listing 10.24 app/controllers/admin/states_controller.rb<br />

def create<br />

@state = State.new(params[:state])<br />

if @state.save<br />

flash[:notice] = "State has been created."<br />

redirect_to admin_states_path<br />

else<br />

flash[:alert] = "State has not been created."<br />

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

end<br />

end<br />

With the create action defined in your Admin::StatesController, you’ll now be able<br />

to run bin/cucumber features/creating_states.feature and have it pass:<br />

1 scenario (1 passed)<br />

8 steps (8 passed)<br />

Very good! By implementing a feature that lets the admin users of your site create<br />

states, you’ve provided a base to build the other state features upon. You shouldn’t<br />

have broken anything by these changes but it won’t hurt to run rake cucumber:ok<br />

spec to make sure. You should see the following summaries:<br />

50 scenarios (50 passed)<br />

571 steps (571 passed)<br />

# and<br />

25 examples, 0 failures, 9 pending<br />

Good to see everything’s still working. Commit and push this now:<br />

git add .<br />

git commit -m "Added Admin::StatesController for managing states"<br />

git push<br />

With this base defined, you can move on to more exciting things than CRUD, such as<br />

defining a default state for your tickets.<br />

10.4.2 Defining a default state<br />

A default state for the tickets in your application will provide a sensible way of grouping<br />

tickets that are new to the system, making it easier for them to be found. The easiest<br />

way to track which state is the default state is to add a boolean column called<br />

default to your states table, which is set to true if the state is the default, false if not.<br />

To get started, you write a feature that covers changing the default status. At the end<br />

of this feature, you end up with the default field in the states table, and then you can<br />

move on to making the tickets default to this state. Let’s create a new feature called features/managing_states.feature<br />

and fill it with the content from the following listing.<br />

273

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

Saved successfully!

Ooh no, something went wrong!