I tried to summarize devise which was difficult at first glance

Introduction

The encounter with devise was the worst. Even the concept of Gem is ambiguous in the first place, and I remember installing it when I was able to understand the flow of the MVC model a little. In devise, I basically don't touch the controller, so at first glance I couldn't understand why the action was being executed without permission. So, I decided to put together something like "This is all!"

What is devise

devise is a gem that specializes in user management functions. Settings such as sign-in and logout are easy. In other words, you don't have to manually set the routing, controller, and so on. If the application can be registered as a user and receive the service, introducing it has the advantage of speeding up development.

Overall flow

  1. Add to Gemfile
  2. Install devise
  3. Creating a model
  4. Create a table
  5. Link settings for new registration and login
  6. Create a view file for new registration

This time we will only deal with new registrations.

1. Add to Gemfile

Gemfile


gem 'devise'

・ The last line of the file is OK

2. Install devise

There are two things to do here.

① After confirming that the current directory is the directory of the application you want to install, At the terminal

bundle install

Be sure to do this after adding it to the Gemfile. Reboot the local server if necessary. (Control + c → rails s)

② Also at the terminal

rails g devise:install

This command is important. Don't forget! This command allows you to use devise.

3. Create a model

Since it is assumed that the user management function will be implemented this time, the model name will be "user". Also at the terminal

rails g devise:model user


Difficult points
If you are studying reils for the first time, the command when creating a model is

rails g model user

To learn. Comparing the differences, it is the presence or absence of devise. The results differ greatly depending on the presence or absence of this.

If the command doesn't have devise below, it's just creating a model. In other words, devise cannot be utilized. By setting devise: model, even the routing related to devise is generated at the same time with this command alone. It saves you the trouble of creating a route. Surprisingly, this is a difficult point to learn and a good thing about devise. (The created model is in the "app / models" directory)

4. Create table

Add columns to the migration file as needed. This time it is omitted. By the way, devise has an email and password columns by default. This is also a competent point of devise. At the terminal.

rails db:migrate

Here, dvise is not attached. The idea is that migration files are not limited to files created with devise, but there are others.

5. Link settings for new registration and login

Assuming that the link to the view is specified by the helper method link_to. At the terminal

rails routes

Then, the routing is lined up in a row. (The view of rails routes is omitted) What should be noted is sign_up (new registration) sign_in (login) Is a different thing. New registration = registration Login = session Is included in the path.

If you specifically describe the link in the view file, it will be as follows.

<%= link_to 'sign up', new_user_registration_path %>
<%= link_to 'Login', new_user_session_path %>

Anyway, pay attention to the difference in path.

6. Create a view file for new registration

First, from the command to generate a view file. Again, at the terminal

rails g devise:views

This is also a difficult point. There are three important things.

The first is As with the model generation earlier, add "devise". The view file generated by adding devise will be under devise. In particular, app/views/devise/registrations/new.html.erb app/views/devise/sessions/new.html.erb

The second is It is divided into a registrations directory for new registration and a sessions directory for login. In other words, the file that edits the HTML of the new registration screen and the file that edits the HTML of the login screen are different.

The third is The last word of the command is pluralized as view s </ font>. As mentioned in the second section, a number of view files are generated, so you can remember it as the plural.   By the way, the code is written in both the newly registered HTML file and the login HTMl file by default. Edit as needed. (If you have added columns or if you are elaborate on the design)

point

-[x] devise is added in the terminal when generating a model or view file. -[x] New registration and login are different. -[x] Almost no routing or controller description is required, which makes development easier.

Finally

The text has become long and difficult to read. There is no doubt that you will feel a convenient Gem once you get used to it!

Recommended Posts

I tried to summarize devise which was difficult at first glance
I tried to summarize again the devise that was difficult at first sight
I tried to summarize iOS 14 support
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to summarize the methods used
I tried to summarize Java lambda expressions
I tried to summarize the Stream API
What is Docker? I tried to summarize
I tried to summarize about JVM / garbage collection
[Must see !!!] I tried to summarize object orientation!
I tried to summarize the state transition of docker
I tried to summarize various link_to used this time
I tried to summarize the basics of kotlin and java
I was addicted to setting default_url_options with Rails devise introduction
I tried to summarize the basic grammar of Ruby briefly
I tried to summarize personally useful apps and development tools (development tools)
I tried to summarize personally useful apps and development tools (Apps)
I tried to summarize object orientation in my own way.
I tried to verify yum-cron
[Circle CI] A story I was addicted to at Start Building
I tried to summarize the words that I often see in docker-compose.yml
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to summarize the methods of Java String and StringBuilder
I tried to make Numeron which is not good in Ruby
Special Lecture on Multi-Scale Simulation: I tried to summarize the 7th
I tried running gRPC's Quick Start (Kotlin version), but it was difficult to read the Gradle script.
I was addicted to starting sbt
I tried to chew C # (indexer)
I tried to interact with Java
I tried to understand nil guard
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried to summarize the stumbling points when developing an Android application
I tried to summarize the key points of gRPC design and development
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I tried AWS Copilot CLI which can build ECS at high speed
[After learning Progate] I tried to summarize form_with while comparing with form_tag
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②