[Rails] How to get the user information currently logged in with devise

Introduction

With devise, you can "get information about the currently logged in user". For example, we will show you how to display information already registered by the target user, such as "name", "date of birth", "address", and "phone number".

table of contents

  1. current_user method
  2. View of transition destination

Development environment

ruby 2.6.5 rails 6.0.0 devise 4.7.3

Premise

devise installed model generated Registered as a user and already have data view created

Implementation

Let's implement it.

1. current_user method

You can get the information of current_user (currently logged in user) with the method that can be used because devise is installed.

index.html.erb


<%= link_to current_user.nickname, edit_user_registration_path(current_user), class: "user-name" %>
          #↑ Here#                           #↑ Here#

For example, suppose you want to edit user information on My Page. If you write current_user.name in the link_to method, the name saved in the name column of the user who is currently logged in will be displayed. Also, by adding current_user after the path specified by url, the user information can be displayed at the transition destination as well.

2. View of transition destination

On the view page of the transition destination, the information of the already registered user is displayed.

edit.html.erb


<%= form_with model: @user, url: edit_addresses_path,  class: 'registration-main', local: true, method: :get do |f| %>

<%= f.text_area :name, class:"input-default", id:"name", placeholder:"Example)Taro", maxlength:"40" %>

<% end %>

Since I want to create a page to edit user information this time, edit the file edit.html.erb in app> views> devise> registrations. Use the form_with method. Since @user is defined inside devise and data has already been assigned, use "model: @user". 「do |f|By setting "", "f" up to "end".text_area :The current user information is displayed in "Column name (name this time)".

Summary

This is how to get the user information that you are currently logged in to. The point is to use the current_user method.

Finally

I am a beginner in programming, but I am posting an article in the hope that it will help people who are similarly troubled. See you next time ~

Recommended Posts

[Rails] How to get the user information currently logged in with devise
[Rails] Use devise to get information about the currently logged in user
How to get the ID of a user authenticated with Firebase in Swift
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
How to get along with Rails
[Rails] How to display information stored in the database in view
[rails devise] How to transition users who are not logged in to the login page
How to get the date in java
How to get boolean value with jQuery in rails simple form
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
[Rails] How to log in with a name by adding a devise name column
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
How to check Rails commands in the terminal
[Ruby on Rails] How to log in with only your name and password using the gem devise
[Rails] How to register multiple records in the intermediate table with many-to-many association
[Rails] How to operate the helper method used in the main application with Administrate
How to set the display time to Japan time in Rails
How to update devise user information without a password
Introduce devise in Rails to implement user management functionality
How to get information about associated tables in many-to-many tables
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
How to query Array in jsonb with Rails + postgres
[Rails] How to get the contents of strong parameters
How to get started with creating a Rails app
[Rails] How to display an image in the view
[Rails] How to install devise
Edit user information with Devise
How to get the class name / method name running in Java
How to get values in real time with TextWatcher (Android)
How to get the longest information from Twitter as of 12/12/2016
[With back tricks] How to introduce React to the simplest Rails
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[rails] Problems that cannot be registered / logged in with devise
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
How to get started with slim
[rails] How to display db information
[Rails] How to write in Japanese
How to get parameters in Spark
How to introduce jQuery in Rails 6
Memo that transitions to the login screen if you are not logged in with devise
How to install Swiper in Rails
[Rails / Routing] How to refer to the controller in the directory you created
[Rails] How to get the URL of the transition source and redirect
[rails] How to display parent information in child view in nested relationships
[Rails] How to introduce kaminari with Slim and change the design
How to rename a model with foreign key constraints in Rails
How to store the information entered in textarea in a variable in the method
[Rails] How to reset the database in production environment (Capistrano version)
How to get the length of an audio file in java
[Rails 5] How to display the password change screen when using devise
How to write the view when Vue is introduced in Rails?
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
How to implement search functionality in Rails
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to insert a video in Rails
[Rails] How to use the map method