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".
ruby 2.6.5 rails 6.0.0 devise 4.7.3
devise installed model generated Registered as a user and already have data view created
Let's implement it.
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.
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)".
This is how to get the user information that you are currently logged in to. The point is to use the current_user method.
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