Sign In Anywhere with Devise – Ruby on Rails
Tags: ruby, rails, tutorial, video
Published: 2019-10-31 19:52:31

---
title: Sign In Anywhere with Devise – Ruby on Rails
published: true
tags: ruby, rails, tutorial, video
canonical_url: https://web-crunch.com/sign-in-anywhere-devise-ruby-on-rails/
---

The [Devise gem](https://github.com/plataformatec/devise) ships with a default configuration that allows you to sign in on a specific route. This post address the idea of changing that guideline to allow a user to sign in anywhere.

{% youtube aJ6XXxU1Zgs %}

### Initial Setup

For the purposes of this demo, I used a Ruby on Rails application template I made called [kickoff\_tailwind](https://github.com/justalever/kickoff_tailwind). It leverages a few bells and whistles and ultimately saves me time creating and configuring Rails apps. Configuring Devise is one of those time savers that my application template takes care of for me. I also extend the `User` model to include `name`, `username`, and `admin` attributes. These don’t come by default with Devise.

All this is to say you’re welcome to use my template or roll a new Rails app from scratch. In the video, you’ll see me reference my template specifically.

### Extending Devise logic

The easiest way to allow your login form to appear elsewhere in a given Ruby on Rails application is to include some methods that Devise depends on when rendering its forms. If you installed the devise views as I have you can see inside `app/views/devise/sessions` there is a template for rendering the login form specifically.

I’ll copy this and paste it into `app/views/shared/_login.html.erb`. This is a new folder and file I created which can easily be rendered anywhere in our app. To promote reusability I made it a partial in a new `shared` folder.

Rendering the template on `localhost:3000` now results in an error. Devise expects some variables that aren’t available. We can approach adding these methods/variables in a number of ways but the easiest is to add it as helper logic in `app/helpers/application_helper.rb`. That way we have access to these virtually anywhere in the application.

```ruby
# app/helpers/application_helper.rb

module ApplicationHelper
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def resource_class
    User
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

```

With the methods above-defined you should be able to reload your root path and see the login form. I go into greater detail about what’s happening and how to conditionally render the login form in the video. This concept makes logging in a breeze and ultimately doesn’t force you to use the Devise gem defaults. Pretty cool stuff!

### The Series So Far

- [Let’s Build: With Ruby on Rails – Extending Devise Series – Adding Custom Fields](https://web-crunch.com/extending-devise-custom-fields)
- [Let’s Build: With Ruby on Rails – Extending Devise Series – Confirmation Emails](https://dev.to/justalever/let-s-build-with-ruby-on-rails-extending-devise-series-confirmation-emails-egf)
- [Let’s Build: With Ruby on Rails – Extending Devise Series – Custom Routing](lets-build-with-ruby-on-rails-extending-devise-custom-routing)
- [Let’s Build: With Ruby on Rails – Extending Devise Series – Extending Devise – Login With Username or Email](https://web-crunch.com/devise-login-with-username-email)

### Shameless plug time

[![](https://i1.wp.com/web-crunch.com/wp-content/uploads/2019/01/YouTube-Promo3.jpg?resize=3840%2C2160&ssl=1)](http://hellorails.io)

I have a new course called [Hello Rails](https://hellorails.io). Hello Rails is a modern course designed to help you start using and understanding Ruby on Rails fast. If you’re a novice when it comes to Ruby or Ruby on Rails I invite you to check out the site. The course will be much like these builds but a super more in-depth version with more realistic goals and deliverables. [Download your copy today!](https://hellorails.io/)!

Follow [@hello\_rails](https://twitter.com/hello_rails) and [myself @justalever](https://twitter.com/justalever) on Twitter.

The post [Sign In Anywhere with Devise – Ruby on Rails](https://web-crunch.com/sign-in-anywhere-devise-ruby-on-rails/) appeared first on [Web-Crunch](https://web-crunch.com).