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.

154 CHAPTER 7 Basic access control<br />

Using the following listing, create the form partial that’s referenced in this new view at<br />

app/views/admin/users/_form.html.erb. It must contain the email and password<br />

fields, which are the bare essentials for creating a user.<br />

Listing 7.23 app/views/admin/users/_form.html.erb<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

For this form_for, you use the array form you saw earlier with [@project, @ticket],<br />

but this time you pass in a symbol rather than a model object. Rails interprets the<br />

symbol literally, generating a route such as admin_users_path rather than<br />

users_path, which would normally be generated. You can also use this array syntax<br />

with link_to and redirect_to helpers. Any symbol passed anywhere in the array is<br />

interpreted literally.<br />

When you run the feature once again, you’re told there’s no action called create:<br />

And I press "Create User"<br />

The action 'create' could not be found for Admin::UsersController<br />

7.5.4 The create action<br />

Let’s create that action now by using this code:<br />

def create<br />

@user = User.new(params[:user])<br />

if @user.save<br />

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

redirect_to admin_users_path<br />

else<br />

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

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

end<br />

end<br />

With this action implemented, both scenarios inside this feature now pass:<br />

2 scenarios (2 passed)<br />

21 steps (21 passed)<br />

This is another great middle point for a commit, so let’s do so now. As usual, you<br />

should run rake cucumber:ok spec to make sure everything’s still working:

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

Saved successfully!

Ooh no, something went wrong!