Setting up Devise and Users

Premium Content - Free Preview

Setting up Devise

Once the Devise gem is installed, you can use it in your app. The first step is to run the Devise generator in the terminal:

rails generate devise:install
create  config/initializers/devise.rb
create  config/locales/devise.en.yml

As you can see, this creates two files. devise.rb contains configuration options, but we'll leave it as is for now. Devise displays some additional steps for setting things up, but you don't need to do everything it mentions. Let's go through the steps:

Some setup you must do manually if you haven't yet:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

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

     In production, :host should be set to the actual host of your application.

This step is for sending email, but we'll ignore it for now. Later, you'll want to configure it correctly so your app sends email from the right domain.

2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"

You already set a root URL in routes, so you don't need to do anything now.

3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

       <p class="notice"><%= notice %></p>
       <p  class="alert"><%= alert %></p>

Flash Messages

After a user successfully submits in a form, your site should display a one-time message to the user to tell them that their form was accepted. If there was an error with their form, the site should display the error information.

End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Contact Us
Sign in or email us at [email protected]