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.

Fixing what you broke<br />

Post, put, delete, and anything<br />

You can define routes that respond only to POST, PUT, and DELETE requests using<br />

the post, put, and delete methods, respectively. All of these methods use the<br />

same syntax and work in similar manners, but they define routes that respond only<br />

to certain HTTP methods.<br />

If it doesn’t matter which HTTP method a route responds to, you can use the match<br />

method:<br />

match '/some_route',<br />

:to => "some#where"<br />

This would respond to GET, POST, PUT and DELETE methods. This method is actually<br />

used by the get, post, put, and delete methods internally, except they call it like this:<br />

match '/some_route',<br />

:to => "some#where",<br />

:conditions => { :method => :get }<br />

You could use conditions to filter the HTTP methods to which a route would respond,<br />

but it’s better to just use the relevant HTTP method’s method.<br />

The get method defines a new route in your application that responds to only GET<br />

requests to /awaiting_confirmation. This route goes to the confirmation action in<br />

UsersController, which you haven’t created yet either. Soon!<br />

The :as option tells Rails that you want routing helpers generated for this route<br />

and you want the helpers to have the prefix confirm_user. This generates<br />

confirm_user_path, which you use in your new check_for_sign_up action in<br />

ProjectsController as well as in a confirm_user_url method.<br />

When you run the Signing Up feature again with bin/cucumber features/signing<br />

_up.feature you don’t get the same error, but you get one that can be easily fixed:<br />

Given I am on the homepage<br />

uninitialized constant UsersController<br />

This is the controller you’ll use to show users the confirmation page, so let’s create it<br />

with a confirmation action that you’ll need with this command:<br />

rails g controller users confirmation<br />

This command adds a route to your routes file that you don’t want (because it overrides<br />

a route that Devise uses), so remove this line from config/routes.rb:<br />

get "users/confirmation"<br />

The added bonus of putting the action here is that you get a view for free at app/<br />

views/users/confirmation.html.erb. In this view you’ll display the “You have signed up<br />

successfully” message as well as a “Please confirm your account before signing in” message.<br />

Before you add these messages to the template, add a line at the bottom of the<br />

scenario inside features/signing_up.feature to check for this confirmation message:<br />

Then I should see "Please confirm your account before signing in."<br />

181

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

Saved successfully!

Ooh no, something went wrong!