[Rails] How to edit and customize devise view and controller

Introduction

How to edit and customize devise view and controller

After introducing devise, I want to prepare a view that looks bland and is only in English. You need to edit and customize the devise controller. I will briefly introduce the method in such a case.

table of contents

  1. Premise
  2. Change the configuration file of devise.rb
  3. Generator generation / customization
  4. Generate / edit view file
  5. Summary

Development environment

ruby 2.6.5 rails 6.0.0 devise 4.7.3

1. Premise

devise installed model generated Routing is set everywhere

Implementation

Let's implement it.

2. Change the configuration file of devise.rb

Open config / initializers / devise.rb. Enable config.scoped_views.

config/initializers/devise.rb



  # ==> Scopes configuration
  # Turn scoped views on. Before rendering "sessions/new", it will first check for
  # "users/sessions/new". It's turned off by default because it's slower if you
  # are using only default views.
  config.scoped_views = true #← Default is false#

Restart the Rails server.

3. Generate / edit controller file

Generate a devise controller.

$ rails g devise:controllers users

The lecture at this time is rails g devise: controllers model name is. This time I'm using the devise model as user, so I'll use that name. app > controllers > users > You will have a file under. スクリーンショット 2020-10-10 11.27.44.png For example, if you want to customize the controller related to user registration, edit the registrations_controller.rb file.

One thing to note here is the routing settings. Described in routing

[Controller name] # [Action name] And the actual [Controller] # [Action]

If it is different, it is natural, but it does not work. Therefore, it is recommended to check it carefully with rails routes etc.

4. Generate / edit view file

Generate and edit the devise view file.

$ rails g devise:views

app > views > devise > A folder will be created directly under devise.

スクリーンショット 2020-10-10 11.04.57.png

For example, if you want to edit the registration screen, edit the file in the registrations directory.

The thing to note here is the view filename. In devise, if you do not specify the transition destination with render or redirect_to, it will transition to the method name after processing of the controller. So you need to match each name.

5. Summary

Introduced the generation / customization of contoroller and the generation / editing of view files. Using devise's controller has the advantage that you can automatically use the functions that you have by default. However, on the other hand, even if you make a mistake, devise will work behind the scenes, so you will not behave as you want and you will not get an error. For that reason, it is recommended to pay attention to how the routing, controller, and view are following.

Finally

I am a beginner in programming, but I am posting an article in the hope that it will help people who want to become engineers as well as myself. See you next time ~

Recommended Posts

[Rails] How to edit and customize devise view and controller
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Note] How to use Rails 6 Devise + cancancan
[Caution !!] Precautions when converting Rails devise and view files to haml
[Rails] How to translate devise into Japanese
How to separate .scss by controller in Rails
How to build API with GraphQL and Rails
[Rails] How to get success and error messages
Rails scope anti-patterns and how to eliminate them
How to write Rails
[Rails] Customize devise validation
[Rails] How to load JavaScript in a specific view
(Ruby on Rails6) How to create models and tables
[Rails] How to display an image in the view
[Rails] How to use enum
How to read rails routes
Relationship between Controller and View
How to use rails join
[Rails] Add column to devise
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
How to use OpenCV 4 on Android and view camera live view
[Rails] How to define macros in Rspec and standardize processing
[Rails] Differences between redirect_to and render methods and how to output render methods
How to set and describe environment variables using Rails zsh
[Rails] How to delete images uploaded by carrierwave (using devise)
Super easy in 2 steps! How to install devise! !! (rails 5 version)
How to run React and Rails on the same server
[Rails] How to display information stored in the database in view
How to use StringBurrer and Arrays.toString.
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails / Routing] How to refer to the controller in the directory you created
[Rails] How to use flash messages
[Rails] How to get the URL of the transition source and redirect
[rails] How to display db information
How to use EventBus3 and ThreadMode
How to "hollow" View on Android
[Rails] How to prevent screen transition
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to use Ruby on Rails
How to deploy Bootstrap on Rails
How to call classes and methods
[Rails] How to speed up docker-compose
How to use equality and equality (how to use equals)
[Rails] How to add new pages
How to connect Heroku and Sequel
[Rails] [Devise] Edit profile without password
[Rails] How to write exception handling?
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws