[Rails] Add column to devise

Why i wrote

When you create user authentication with devise, you will have email and password from the beginning, but you need a name to register as a user. I always think, so take a note.


--Already added the devise gem --It must be rails g devise User

Add a column

When adding a column to the DB, it is common to write as follows. For now, give the migration file a name that tells you what the migration file does.

When naming it, if you write ʻAddColumnToUsers` and it means something like" add a column to the user table ", be careful because the name will be a problem when you add the next column. In the first place, I think that it is necessary to add columns = DB design, but I am still inexperienced in this area, so please tell me who is strong.

$rails generate migration Add Column name To Table name Column to add:Data type
$ rails generate migration AddNameToUsers name:string


class AddNameToUsers < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :name, :string
$ rails db:migrate

Fix Controller and View


<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

	#Add from here
 <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, autofocus: true %>
	#So far

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "new-password" %>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password" %>

  <div class="actions">
    <%= f.submit "Sign up" %>
<% end %>

<%= render "devise/shared/links" %>

I want to be able to receive the information of the name that flew from View, so Add the following to application_controller.rb.


class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])

Write the following code in ʻapplication.html.erb. current_user` is one of the methods provided by devise, and you can get the information of the logged-in user.


<% if current_user.present? %>
  <p>Hello,<%= current_user.name %>San!</p>
<% else %>
  <p>Hello, Guest!</p>
<% end %>

If it is displayed well, it's OK!

スクリーンショット 2020-07-22 17.47.14.png

Recommended Posts

[Rails] Add column to devise
[Rails] Add strong parameters to devise
[Rails] devise
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to add new pages
[Rails] Processing after adding a column to the devise table
Add binding.pry (rails)
Migration file to add comment to Rails table
Add foreign key to column with migrate
[Rails] Introducing devise
[Rails] Add a confirmation screen and a completion screen to devise membership registration.
[Rails] [Memo] When to add = to <%%> and when not
I want to add devise in Rails, but I can't bundle install
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to translate devise into Japanese
[Rails] How to create a table, add a column, and change the column type
The road to Japaneseizing Rails devise error messages
Add a tag function to Rails. Use acts-as-taggable-on
How to conditionally add html.erb class in Rails
[Rails] Added in devise: username not added to database
Add files to jar files
How to write Rails
[Rails] devise helper method
Introducing Bootstrap to Rails 5
[Rails] Customize devise validation
Introducing Bootstrap to Rails !!
Introduce Vue.js to Rails
Handle devise with Rails
How to uninstall Rails
[Rails] devise introduction method
[rails] About devise defaults
[rails] How to use devise helper method before_action: authenticate_user!
Introduce devise in Rails to implement user management functionality
[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
[Rails] How to change the column name of the table
I want to authenticate users to Rails with Devise + OmniAuth
[Rails] How to edit and customize devise view and controller
[Rails] Add Tags that are related to has_many to Devise User (addition of function to follow tags)
[rails] How to post images
[Rails] Introduction of devise Basics
What is Rails gem devise?
Add an icon to the header link using Rails fontawesome
[Rails] gem devise installation flow
[Rails] How to use enum
[Ruby on Rails] Add a column with a foreign key constraint
[Rails] How to use enum
I was addicted to setting default_url_options with Rails devise introduction
4 Add println to the interpreter
(For beginners) [Rails] Install Devise
How to terminate rails server
How to write Rails seed
[Rails] How to use validation
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[Rails] How to disable turbolinks
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
Pass parameters to Rails link_to
[Rails] How to use authenticate_user!
[Rails] How to implement scraping
[Rails] How to make seed