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.

272 CHAPTER 10 Tracking state<br />

Listing 10.23 app/views/admin/states/index.html.erb<br />

<br />

<br />

<br />

<br />

<br />

<br />

With this view written, your feature will now whinge about the new action when you<br />

run bin/cucumber feature/creating_states.feature:<br />

And I follow "New State"<br />

The action 'new' could not be found<br />

for Admin::StatesController (AbstractController::ActionNotFound)<br />

Alright then, you should add the new action to Admin::StatesController if you want<br />

to continue any further. It should be defined like the following inside that controller:<br />

def new<br />

@state = State.new<br />

end<br />

You now need to create the view for this action at app/views/admin/states/<br />

new.html.erb and fill it in with the following content:<br />

New State<br />

<br />

You’re using a form partial here again because it’s best practice and also just in case<br />

you ever wanted to use it for an edit action. In a new file for your partial at app/views/<br />

admin/states/_form.html.erb, you put the form that will be used to create new states.<br />

This form is pretty simple—it only needs a text field for the name and a submit button<br />

to submit the form:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Because the @state variable coming from the new is a new instance of the State<br />

model, the submit method will display a submit button with the text Create State, just<br />

like your feature needs. Speaking of which, with this form partial done, your feature<br />

should run a little further. You should check this now by running bin/cucumber<br />

features/creating_states.feature:<br />

And I press "Create State"<br />

The action 'create' could not be found<br />

for Admin::StatesController (AbstractController::ActionNotFound)

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

Saved successfully!

Ooh no, something went wrong!