Adding Links


Collapse Content

Devise Paths

Navigate to /rails/info/routes to see the new paths that Devise created. Here are the paths Devise created for signing up, signing in and signing out:

Path Helper HTTP Verb Path Controller#Action
new_user_session_path GET /users/sign_in(.:format) devise/sessions#new
user_session_path POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session_path DELETE /users/sign_out(.:format) devise/sessions#destroy
user_registration_path POST /users(.:format) devise/registrations#create
new_user_registration_path GET /users/sign_up(.:format) devise/registrations#new

HTTP Verbs

Every web request (such as going to products/3) has an HTTP verb (or "method") associated with it, which explains the action the user wants to perform. The most common verb is GET, to get a web page or file without changing anything. As you can see above, there are other HTTP verbs; here's what each request is for:

  • POST - create a resource, such as a new user or product
  • GET - display a resource on a site, such as a product information
  • PUT - update a resource (we don't need that yet)
  • DELETE - delete a resource, such as a user or product

As mentioned in Standard Resources, Rails provides a standard way to map URLs to actions. These 4 HTTP actions correspond to the 4 standard CRUD operations - Create, Read, Update, Delete. Rails helps you create standardized routes where each requests uses the appropriate verb. When your routes follow this standardized format, they are called RESTful resources.

Creating Links

Let's create links for signing in and up on the site. We already created a header partial, so let's open it up:

layouts/_header.html.erb

<header class="navbar">
    <%= link_to "Home", root_path %>

    <div class="u-pull-right">
        <%= link_to "About", about_path %>
    </div>
</header>

The right side looks like a good place to add links for signing up and logging in to the site, so can you go ahead and add two links? Use the sign_in and sign_up paths to create the links.

The Code

If you're not yet signed in, click on the link and sign in to your site. After signing in, you may notice an issue: The signup and sign-in links are still displayed even though you're signed in. If a user is signed in, we should display a logout link instead:

layouts/_header.html.erb

<header class="navbar">
    <%= link_to "Home", root_path %>

    <div class="u-pull-right">
        <%= link_to "About", about_path %>

        <% if user_signed_in? %>
            <%= link_to "Logout", destroy_user_session_path, method: :delete %>
        <% else %>
            <%= link_to "Sign up", new_user_registration_path  %> |
            <%= link_to "Login", new_user_session_path %>
        <% end %>
    </div>
</header>

This code uses Devise's helper method user_signed_in? which returns true only if the user is signed in. When that's the case, it display a "Logout" link for the user to sign out. Notice the link has an extra option at the end: method: :delete. A standard link sends an HTTP GET requests, but in this case you want to send a delete request to match the path for deleting resources. The Rails link_to method lets you specify the HTTP method to use.

Q: What is created when a user signs in and what is deleted when they sign out?
A: When a user signs in, Rails creates a session to keep track of them. This sessions identifies the user and can store other associated information. When a user logs out, their session is destroyed.

(See the Rails Security Guide for more information about Sessions.)

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