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.

118 CHAPTER 6 Authentication and basic authorization<br />

To round out the chapter, you’ll create another CRUD interface, this time for the<br />

users resource, but with a twist.<br />

Before you start, you must set up Devise!<br />

6.1 What Devise does<br />

Devise is an authentication gem that provides a lot of the common functionality for<br />

user management, such as letting users sign in and sign up, in the form of a Rails<br />

engine. An engine can be thought of as a miniature application that provides a small<br />

subset of controllers, models, views, and additional functionality in one neat little<br />

package. You can override the controllers, models, and views if you wish, though, by<br />

placing identically named files inside your application. This works because Rails looks<br />

for a file in your application first before diving into the gems and plugins of the application,<br />

which can speed up the application’s execution time.<br />

6.1.1 Installing Devise<br />

To install Devise, first add the following line to the Gemfile, right after the end for the<br />

:test group:<br />

gem 'devise', '~> 1.4.3'<br />

To install the Devise gem, run bundle. Once Devise is installed, you need to run the<br />

generator, but how do you know the name of it? Simple! You can run the generate<br />

command with no additional arguments to list all the generators:<br />

rails generate<br />

In this output, you’ll see devise:install listed. Hey, that’ll probably help you get<br />

things installed! Let’s try it:<br />

rails g devise:install<br />

This code generates two files, config/initializers/devise.rb and config/locales/<br />

devise.en.yml:<br />

� config/initializers/devise.rb sets up Devise for your application and is the source<br />

for all configuration settings for Devise.<br />

� config/locales/devise.en.yml contains the English translations for Devise and is<br />

loaded by the internationalization (I18n) part of Rails. You can learn about<br />

internationalization in Rails by reading the official I18n guide: http://<br />

guides.rubyonrails.org/i18n.html.<br />

The code also gives you three setup tips to follow. The first setup tip tells you to set up<br />

some Action Mailer settings, which you can place in your development environment’s<br />

configuration at config/environments/development.rb and in your test environment’s<br />

configuration at config/environments/test.rb:<br />

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

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

Saved successfully!

Ooh no, something went wrong!