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.

180 CHAPTER 8 More authorization<br />

Create a new controller now at app/controllers/registrations_controller.rb and fill it<br />

with this content:<br />

class RegistrationsController < Devise::RegistrationsController<br />

private<br />

def after_inactive_sign_up_path_for(resource)<br />

confirm_user_path<br />

end<br />

end<br />

By defining this new controller as inheriting from Devise::Registrations-<br />

Controller, you inherit all the behavior from that controller and gain the ability to<br />

override things such as the after_inactive_sign_up_path_for, of which you take<br />

full advantage here. The resource argument is the User object representing who’s<br />

signing up. You could use it, but in this context, don’t. Next, you need to tell Devise to<br />

use this controller instead of its own. Alter the following line in config/routes.rb<br />

devise_for :users<br />

to this:<br />

devise_for :users, :controllers => { :registrations => "registrations" }<br />

The :controllers option tells Devise you want to customize the controllers it uses,<br />

and with this new hash, you tell it to use the RegistrationsController you defined<br />

for registrations. In this controller, you override after_inactive_sign_up_path_for<br />

to go to a new route: confirm_user_path.<br />

Because you’re overriding Devise’s controller, Rails won’t use Devise’s views. You<br />

must copy the views from Devise into your application and move them into the app<br />

/views/registrations directory. Lucky for you, Devise has a generator that places<br />

Devise’s views in your application: devise:views. You can run the generator with<br />

this command:<br />

rails g devise:views<br />

This command places Devise’s views into the app/views/devise directory of your application.<br />

This directory shares the same name as the directory internal to Devise where<br />

these views came from, and if a view exists in your application first, then Rails doesn’t<br />

look for it in the engines attached to the application. With these files copied over,<br />

move the app/views/devise/registrations directory out to app/views/registrations so<br />

you have some views to use for your new RegistrationsController.<br />

Now you must address the problem that although the confirm_users_path<br />

method used in your RegistrationsController isn’t defined yet, redirect_to takes<br />

users to that location. Define a route for it by opening config/routes.rb and inserting<br />

this line underneath devise_for :users:<br />

get '/awaiting_confirmation',<br />

:to => "users#confirmation",<br />

:as => 'confirm_user'

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

Saved successfully!

Ooh no, something went wrong!