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!"
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.
This time we will only deal with new registrations.
Gemfile
gem 'devise'
・ The last line of the file is OK
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.
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)
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.
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.
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)
-[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.
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!